3

I'm getting a strange behavior in Visual Studio 2015. I'm writting an application targeting 4.5.2 .Net Framework:

.Net version

Then in the application, I try use some things which were implemented in C# 6.0 (which is from .Net 6.0) :

int number = 456;
var name = nameof(number);
string str = $"Time = {DateTime.Now}";

I'm quite sure that nameof and string formatting using $ are only available in .Net 4.6, but still I'm able to compile this code - event though I'm targeting 4.5.2.

I've run the same code in Visual Studio 2013, also targeting the 4.5.2 version, but this time I can't compile the code (nameof not found).

How is it possible, that VS2015 allowed me to compile the code which shouldn't be allowed in target .Net Framework version?

My shoot in the dark is that the VS2015 is capable of computing the C# 6.0 code into the CIL which is still compatible with previous versions, but I'm not sure if it's the right guess.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Kamil T
  • 2,232
  • 1
  • 19
  • 26

1 Answers1

9

C# 6 is a new version of language C#, it has nothing to do with .NET Framework version. So you can use C# 6 even with old version's of .NET. Roslyn compiler is a new compiler that is shipped with VS2015, but could be installed on older versions of VS as standalone. So basically Roslyn compiles the new features for you, regardless the framework version you are using.

Community
  • 1
  • 1
3615
  • 3,787
  • 3
  • 20
  • 35