Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 7 .NET / CLR / Visual Studio version requirements

What are the minimum .NET framework and CLR version requirements for running C# 7? Also, do I need VS 2017 to compile C# 7?

like image 339
ilias Avatar asked Mar 08 '17 13:03

ilias


2 Answers

To use the full power of C# 7 out of the box (without referencing NuGet packages and so on) you need VS 2017 and .NET 4.7 as the Target Framework.

like image 29
SerG Avatar answered Nov 20 '22 16:11

SerG


You do NOT need to target .NET 4.6 and above, that is incorrect. To use Tuples, you need the System.ValueTuple NuGet package. Right on https://www.nuget.org/packages/System.ValueTuple/ you can see it says it supports 4.5 and above, and actually, it supports 4.0 and above. And if you want to get crazy, if you create your own System.ValueTuple class that does exactly what that package does, it will work back on .NET 3.5 and probably older too. For "Task-like" types, you also need a NuGet package, https://www.nuget.org/packages/System.Threading.Tasks.Extensions/. This package also works on .NET 4.5 and newer according to its documentation.

Other C# 7 features will just work on .NET 2 and above as they are just syntactic sugar. For example, I just wrote the following in .NET 2.0 and it correctly throws:

static void Main(string[] args)
{
    string test = null;
    string d = test ?? throw new ApplicationException("test");
}

Likewise, int.TryParse("123", out int i); works just fine in .NET 2.0.

I did not test every single C#7 feature, but in general, with the exception of Tuples (and their related features like deconstruction), it should work in .NET 2.0 and above as most of it is just syntactic sugar. That being said, yes you need VS2017 to compile C#7. I'm sure at some point other compilers will support C#7 but not today.

Features I confirmed work in .NET 2.0:

  • Binary Literals
  • Digit Separators
  • Inline out parameters
  • Using _ to discard out parameters
  • Local functions
  • Type based pattern matching if (obj is int i) and case int i:
  • Constant pattern matching if (i is 2)
  • Var pattern matching if (i is var j)
  • Ref returns
  • Throw expressions
  • Expression bodied getters and setters
  • Expression bodied constructors and finalizers
like image 103
dmeglio Avatar answered Nov 20 '22 17:11

dmeglio