C# 7 .NET / CLR / Visual Studio version requirements
Asked Answered
I

2

50

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

Installation answered 8/3, 2017 at 13:42 Comment(2)
Any .NET version 4.6.x is fine. Roslyn is open source so anything is possible. If you want somebody to do it for you then download VS2017. It was released yesterday. The Community edition is free.Cantata
You can use Visual Studio 2015 - see, e.g. this question.Pr
C
99

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
Chartreuse answered 8/3, 2017 at 15:14 Comment(4)
Why the downvote? What is inaccurate about my answer?Chartreuse
Upvote from me, but it's worth reading this answer, as a nuget package is needed for the new async features too.Draper
@DavidArno Thank you, forgot about that feature. I updated to include information about that nugget package as well. Unfortunately, there has been so many things "considered" for C# 7, it's hard finding a list of exactly what was implemented as the Release Notes for VS2017 seem incomplete only mentioning the biggest feature additions.Chartreuse
Re it being difficult to find what has really been put in C# 7, rarely has a more true word been spoken. Sadly, I suspect many of those articles, written months ago, will never be updated be updated to reflect the real feature list.Draper
A
5

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.

Atropos answered 28/8, 2017 at 14:44 Comment(3)
I removed the 'Please, comment on downvote' from your answer. That's a comment, not part of your answer. But to guess at an reason for the downvote, I'd go with this: a) you've not provided any proof for your claims, b) it's wrong (you can target .NET 4.6.2 and below by adding the System.ValueTuple Nuget package - see this answer.Pr
@WaiHaLee Not providing a proof does not make answer wrong. But thank you for pointing this out. You say "it's wrong (you can ... by adding the package)" when my answer contains phrase "without referencing NuGet packages".Atropos
Sorry - yes: you're right - you did point out the NuGet package requirement. Somehow that slipped my attention.Pr

© 2022 - 2024 — McMap. All rights reserved.