C # 3.0 Targeting 2.0

I don’t understand how this works - I am using VS2008 and using 3.0 language features like var keyword.

However, I can compile and run with version 2.0 version

How is this possible?

EDIT: Is there a website that defines CLR, Framework, and language functions and backward compatibility - I'm completely confused by the fact

+7
c #
source share
7 answers

Some language functions are just a compiler that is smart - var one of them. The compiled code has no trace of the fact that the variable was declared via var .

Other functions (for example, extension methods) require support from the framework. Extension methods are recognized and advertised through ExtensionAttribute . Like expression trees, the Expression class and its subclasses are required.

Some other features also require CLR support — the most obvious example is C # 2 generalizations. None of the C # 3 features require CLR support; .NET 3.5 ships with the CLR service pack, but no major changes. I suspect that there are several angular cases where in CL2 v2 there were problems with some expression trees before. (I think DynamicMethod has changed a bit inside, although I cannot recall the details.) There may be some verifiable settings.

I have an article that describes what features in C # 3 you can use when setting up .NET 2. I will expand this to include C # 4 soon.

+9
source share

The reason is that the binary created by the C # compiler using most of the functions of 3.0 is compatible with version 2.0. The CLR does not care about which language or version of the language you are using, only that the binary is compatible with its specifications.

The var keyword is the simplest case as it simply indicates the type of the local variable. The compiler then writes the explicit type to the binary. So the next 2 lines are equivalent since the emitted IL

 var x = 42; int x = 42; 
+3
source share

var , for example, is a compile-time function and is in no way tied to any particular version structure (except that it was introduced with Visual Studio 2008).

Another compile-time function that works in all versions of the framework (a little from my point of view) is optional and has named arguments in Visual Studio 2010. But it makes sense that the compiler can easily generate any methods that are needed.

+2
source share

var is a pure compiler, it does not require any specific runtime support

0
source share

None of the new features in C # 3.0 required any changes in the underlying .NET environment: things like the var keyword, extension methods, and automatic properties lead to code that you could write manually in version 2.0.

In other words, the var keyword is syntactic sugar.

0
source share

The framework version defines only those classes and methods of the library that you can access. var is a fully compiled concept. These two lines are compiled in exactly the same IL:

 var str = String.Empty; string str = String.Empty; 

Similarly, automatic properties are fully compiled. It:

 private string <>SomeRandomName; public string Prop { get { return <>SomeRandomName; } set { <>SomeRandomName = value; } } 

and this:

 public string Prop { get; set; } 

create exactly the same IL (modulo variable names and attributes).

These functions are independent of the classes and methods available in the mscorlib assembly

0
source share

The short answer is that .NET 3.5 functions are all that can be translated into 2.0 by the compiler. Nothing has changed in the structure itself, the compiler has just learned some new tricks.

0
source share

All Articles