2

I have a legacy application that uses .NET 4.

I tried to use a C# 6 feature when I was changing the code, but my visual studio generates the following error:

Feature 'interpolated strings' is not available in C# 4. Please use language version 6 or greater.

enter image description here

I was wondering if there is a way to use the features of the latest versions of C# without updating the version of the framework?

Or is the C# version directly linked with the framework?

obs: updating the framework is not an option.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Deivid Carvalho
  • 311
  • 2
  • 12

3 Answers3

5

The language-version only relies on the version of Visual Studio. To use features intorduced in C#6 you need at least VS2015. This has nothing to do with the installed .NET-framework-version. In fact you can use VS2015 and its compiler-features (e.g. auto-implemented properties with an initial value) and compile against .Net 2.0:

int MyProperty { get; set; } = -1;

To change the target-framework (e.g. .Net 2.0) use Project properties-->Application-->Target framework.

To change the language version via Project properties-->Build-->Advanced-->Language version.

Let´s consider the following example: Beginning with C#3 you can write the following:

var a = new { MyProperty = 1 };

This compiles for any .Net-version. However the most common use-case for anonymous types is when using Linq to fetch data from a database. Linq was introuced in framework-version 3.5 and heavily depends on lambda-expressions and extension-methods, which both were introduced in C#3:

var result = myCollection.Select(x => new { MyProperty = x.MyProperty });

So although you could use anonymous types in earlier versions of the framework, there was little need to do so.

To get a better view on the difference between the C#- (=language)-version and the framework-version read this post. This thread on the other side lists the language-versions and in which version of VS they were released.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
4

The Language version is a Compiler feature, not a runtime one.

Regardless which source code language language or source code language version you use, the end result will always be the same MSIL code. You can freely pick which Framework version to compile against. Which limits your available .NET Libraries. But it does not affect the C# version you are using in any way.

Of course there might be some Language Features that make little sense without supporting libraries. I can just not think of a concrete example of the top of my head.

Edit: Remembered a concrete example: Named and Optional Parameters. IL always supported that. VB.Net had that already for ages. It was a very important part of COM compatibility, especially using the Office COM inter-op. But Prior to C# 4 we C# programmers could not use it: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
Christopher
  • 9,634
  • 2
  • 17
  • 31
  • So, you are saying that every statement (`lock` for example) will always produce the same IL code in the result regardless of target .NET Framework version (4.0 versus 2.0 for example)? – user4003407 Oct 17 '18 at 15:03
  • @PetSerAl lock is more comparable to how a using block works. It is just a shorthand for a try...finally block: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement So it should have the same IL as that specific code. – Christopher Oct 18 '18 at 07:44
  • I know how `lock` work. That is not my question. My question is: does `lock` produce the same IL code whenever I target to .NET Framework 4.0 or 2.0? – user4003407 Oct 18 '18 at 07:49
  • @PetSerAl: I can not answer this with 100% certainty, as I might be missing some miniature details. It should produce the same IL as the actuall statements it is a synonym for. Wich should be the same regardless of targeted Framework version. Not caring about the Input Language was a major design goal of the IL. The input language *version* is just part of general Language Agnosticality. I did remember that concrete example by the way. – Christopher Oct 18 '18 at 07:56
  • Well, it is easy to [check](https://sharplab.io/#v2:C4LglgNgPgAgDAAhgRgNwFgBQMDMTkBsSATAgMIIDeWCtSeKRMALAgLIAUAlFTXfxAD2AYwDWHAHYBTAO4JBAIwBWU4cG5cMmfgF8sOoA===). As you can see it use [`Monitor.Enter(object, ref bool)`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.monitor.enter?view=netframework-4.0#System_Threading_Monitor_Enter_System_Object_System_Boolean__) overload which is not exist in [.NET Framework 3.5](https://docs.microsoft.com/en-us/dotnet/api/system.threading.monitor.enter?view=netframework-3.5). So, it can not use it when targeted .NET Framework 3.5 or below. – user4003407 Oct 18 '18 at 08:14
  • @PetSerAl: The link you posted mentions this as supproted framework versions: 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0 1.1 So your **own link** disagrees with you. it has been around since 1.0. You would have to mix up .NET Framework Version with the .NET Standart or .NET Core Version to even make that mistake, wich is a pretty huge disqualifier for this discussion :) – Christopher Oct 18 '18 at 08:58
  • Please show me the given overload here: https://learn.microsoft.com/en-us/dotnet/api/system.threading.monitor.enter?view=netframework-3.5 Also if you look carefully, then you will see that there is single **Applies to** section not one per overload. The other overload existed since 1.0 and I am never claimed otherwise. – user4003407 Oct 18 '18 at 09:12
  • @PetSerAl: You could have said so from the start. Then it would simply be a information of a exception. And would fall under this sentence: "Of course there might be some Langauge Features that make little sense without supporting libraries." So I am unsure what you point was to formulate this as a question was. Other then hopes of getting me to say something dumb (wich I do know to avoid). – Christopher Oct 18 '18 at 10:01
1

You Need Either Visual Studio 2013 or @ 2015 to use c sharp version 6 u can change the .net version in the project settings. .NET version Wont be a problem