0

How can I determine what version of C# I can use against a particular .NET Framework version?

I have read:

https://stackoverflow.com/a/247623/223742 https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/versions-and-dependencies

However I cannot tell with any degree of certainty what version of C# I can use say against .NET Framework 4.6.1 for instance.

TheEdge
  • 9,291
  • 15
  • 67
  • 135
  • 2
    The C# version is related to the compiler, not the .NET Framework. Check the [second answer](https://stackoverflow.com/a/317885/4934172) to the question you referred to. – 41686d6564 stands w. Palestine Sep 25 '19 at 00:56
  • @AhmedAbdelhameed Right so based on that I can use C# 8.0 against .NET 4.61? Am I correct that VS will complain with a compilation error if I cannot use a feature from 8.0 that is not supported by 4.61? – TheEdge Sep 25 '19 at 00:59
  • 2
    See [this page](https://learn.microsoft.com/en-gb/dotnet/csharp/language-reference/configure-language-version) for information on the default C# version based on the framework – Simply Ged Sep 25 '19 at 01:06
  • 1
    @TheEdge I haven't tried C# 8.0 yet but in general, yes, that's correct. For example, C# 7.1 introduced `async Main`. Now, if you start, let's say, a .NET 2.0 project and tried to use `async Main`, you won't be able to compile the program because of missing Framework features. – 41686d6564 stands w. Palestine Sep 25 '19 at 01:15
  • 1
    Unclear to me why this question is On Hold, especially with 'off-topic' designation. – David Tansey Sep 27 '19 at 01:16

1 Answers1

2

C# version history could be found here.

C# version depends on the compiler version which is shipped with .NET SDK (generally known installed with Visual Studio Installer).

You could still target an .NET implementation (which means .NET Core, Mono, or the obsoleted .NET Framework) of lower version with a project that builds with SDKs of a later version, but some language features are not supported, which are implemented with some built-in types that are not provided by the target .NET implementation. For example, you cannot use the tuple syntax targeting .NET Framework 4.5, even if you build it with the latest .NET SDK, because .NET Framework 4.5 does not have ValueType definitions. But some could be fixed with nuget packages that supplement them such as System.ValueTuple.

C# 8.0 has already been released with .NET Core 3.0.

Alsein
  • 4,268
  • 1
  • 15
  • 35