I read an article about using C# 3 features in C# 2 where you can for instance type var x = 2; and even if the project is a 2.0 project, the Visual Studio 2008 compiler picks it up and generates the same code as it would if you type int x = 2.
But what I don’t get is, should you not do this in some cases? I always thought that the var keyword didn’t arrive until C# 3.. If the compiler generates the same code and I can type C# 3 code and C# 2 code exactly the same, what is the differance really, because the CLI is the same, right?
Quote from the link above
Behind the scenes, the compiler generate regular .NET 2.0 code.
Is there any difference between .NET 2.0 code and .NET 3 code?
I am afraid you are mixing up C# versions and .NET versions.
You cannot use
varin C# 2.0, the compiler will think it is just an identifier. But you can use it in C# 3.0 targeting .NET 2.0, sincevaris just a language (C#) construct, not a .NET construct. The .NET compiler will translate it into the appropriate type in the generated CIL, so the JIT compiler will never see it and you will be completely fine.Since the VS2008 compiler is a C# 3.0 compiler, you will have no problem using
varthere, regardless of the .NET version you are targeting.