tags:

views:

252

answers:

5

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?

+6  A: 

var is syntatatic sugar in the C#3 language which VS2008 uses. the code can still be compiled to be .net 2.0 compatible. The only issue you should have is if you need to edit those files in VS2005.

Sam Holder
Of course it is part of C# 3. It is not part of .NET 3.5, but that's a different thing.
Gorpik
thanks Gorpik, edited the answer
Sam Holder
Ah, I see. That makes sense. So I can still target to .NET 2 with my VS 2008 and let VS 2005 users use my dll's. Thats actually pretty nifty
Patrick
That's absolutely wrong. var keyword is a c# feature, that uses type inference - especially when you're using anonymous types like that of LINQ queries. It's NOT a trick of the IDE.
Josh Smeaton
+1  A: 

about var: it is pure syntax sugar and compiler trick. typing var you ask a compiler to guess type based on right hand side of expression. this can be easily done compiling to .net 2.0. Linq might not work because it uses assemblies from .net 3.5

Andrey
LINQ will work in the sense that the compiler will translate the query expressions to calls to Where, SelectMany etc. Then it will fail to compile if it cannot find any such methods (which would usually indeed happen, since System.Linq.Enumerable does not exist in .NET 2.0), but if you provide your own Where, SelectMany, etc. methods it should bind to them and work.
Joren
+1  A: 

var was introduced to hold the instances of anonymous types in C# 3.0.

In .net2.0 may be the VS2008 or C# compiler must be replacing it with the actual type which is alraedy known at design-time and if you already knew the type while coding why would you use var! :)

this. __curious_geek
`var` does not imply an anonymous type, it only enables it. E.g. `var x = 1;`, x would be int, not an anonymous type.
Allon Guralnek
agreed - var can represent any type. you would not use it for a type already known at coding time, you'd rather use it for anonymous types!
this. __curious_geek
and all I get is a negative vote ??
this. __curious_geek
@this. __curious_geek, That depends on your own preferences. I use var everywhere and I would never explicitly write the correct type. It is more flexible if you use var and it makes refactoring a bit easier, while there isn't any disadvantages with it.
Mattias Jakobsson
I never said there's any disadvantages of doing so but try coding with this approach outside VS2008 IDE text-editor. All you'll see in your code would be var var var everywhere!
this. __curious_geek
@this. __curious_geek, What you seem to say is that you wouldn't use var for known types. What I say is that I do that all the time and that is quite common. I don't care what type it is anyway. And I don't use any IDE that doesn't have intellisense.
Mattias Jakobsson
@this: I downvoted you because your first paragraph, beginning with the word "because", is fundamentally wrong. @Mattias: In my opinion your view of using `var` wherever possible is a bit too extreme for me, as I think it hurts code readability. The IDE does indeed give you hints, but I don't want to rely on them and have to hover over all the local variables just to understand what's going on. My opinion about `var` is to use it for anonymous types (where you must) and for local variable declarations where the type is already specified in the RHS (such as a `new` or a cast).
Allon Guralnek
@Allon: thanks. I agree with your idea and I've updated the statement which was misleading. The idea is to use var only in case of anonymous types.
this. __curious_geek
@Allon, My view is that if you can't figure out what the object is supposed to do by seeing the name of the variable then you need to rename the variable. You don't really need to know the exact type of it anyway, just what you are supposed to do with it.
Mattias Jakobsson
@Mattias, I must respectfully disagree with the views in your last comment.
Allon Guralnek
@Allon, Yes, I understand that some may disagree. And I don't have a problem with that. What I was commenting on was the last statement of the answer. In my opinion it's all about personal preferences and there is no right or wrong.
Mattias Jakobsson
+3  A: 

With respect to it's usage.

It is not bad with regards to execution, as already mentioned it is just syntax sugar and gets replaced at compile time.

When coding, I think it's use is mixed. For example:

I think this ok. It's easy to see what the value is

var a = "test"; 

Not quite so good, many people have to scroll in the IDE to actually understand the code, which is a pain.

var b = SomeReallyLongNameSpace.SubNamespace.Namespace.Class.Enum.Value1; 

This is fine, you know what the result is because a clearly defined method.

var c = GetString(); 

You have no idea at a glance what is returned, this isn't helpful and would be better defined rather than using the var keyword.

var d = GetSomething(); 
Ian
I agree. It's all about readability. If the type that you're assigning to a var is obvious, then use `var`. If it's not obvious, use the type's name. Of course, when it comes to anonymous types you have no choice but to use `var`.
alimbada
Yeah, well. This wasn't really what I asked. There are plenty of questions regarding the keyword var. I wondered why I was able to type it in my Visual Studio 2008 IDE even though my project was targeted to .NET 2. Thanks for the answer though
Patrick
@Patrick, I only skimmed the question... The title was "Is using the keyword var bad in C# 2.0?" and contained within it you asked "But what I don't get is, should you not do this in some cases?". I was trying to highlight some cases where you shouldn't regardless of the build process.
Ian
@Ian: Ah, that's a fair explanation. Perhaps I should have expressed my self differently. What I meant was, if I use `var`, will it break someplace and should therefore not be used.
Patrick
+8  A: 

I am afraid you are mixing up C# versions and .NET versions.

You cannot use var in 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, since var is 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 var there, regardless of the .NET version you are targeting.

Gorpik
Aha! This was the answer I was looking for. This might be another question, but is there any differance between the resulting code between .NET 2 and .NET 3 in that case, i.e. the resulting thing after the compiler does its thing?
Patrick
The CIL is exactly the same as if you had written the type returned by the right hand side expression in your assignment; it does not matter which .NET version you are targetting.
Gorpik
So the difference between .NET 2 and .NET 3 is that .NET 3 packs with a few extra classes (like LINQ) and the syntax is actually the same (if you use a C# 3 compiler like VS2008)?
Patrick
The syntax belongs to the language (C#, in this case), not the framework (.NET). There are some language features that need certain framework capabilities (such as generics), but others don't. For instance, you can use extended methods in .NET 2.0, because the C# compiler will translate them into normal static function calls; now, don't expect .NET generic methods to be present in your .NET 2.0 library, of course, but you could define your own.
Gorpik