Friday, February 11, 2011

Why can't we declare var a = new List<string> at class level?

I know we cannot do this at class level but at method level we can always do this.

var myList=new List<string> // or something else like this

This question came to my mind since wherever we declare variable like this. We always provide the type information at the RHS of the expression. So compiler doesn't need to do type guessing. (correct me if i am wrong).

so question remains WHY NOT at class level while its allowed at method level

  • The compiler guys just didn't implement the support.

    It's entirely compiler magic, and the compiler doesn't actually put something into IL that says "figure out the type at runtime", it knows the type and builds it in, so it could've done that for members as well.

    It just doesn't.

    I'm pretty sure that if you asked an actual compiler guy on the C# compiler team, you'd get something official, but there's no magic happening here and it should be possible to do the same for members fields.

    Jon Skeet : Yup, it could definitely have been done. I suspect it's not implemented for the sake of readability - limiting the type inference with var to local variables means you only ever see it in the context of its use.
    Brian : This is incorrect. See my answer. There are technical issues involved.
  • The var keyword was invented specific to support anonymous types. You are generally NOT going to declare anonymous types at the class level, and thus it was not implemented.

    Your example statement

    var myList=new List<string>
    

    is not a very good example of how to use the var keyword since it's not for the intended purpose.

    Jon Skeet : I disagree. It can make code clearer to read by reducing redundancy. See http://csharpindepth.com/ViewNote.aspx?NoteID=61
    Joseph Daigle : While I agree it would reduce redundancy, that is not the reason it was invented. The language designers didn't intend for the keyword to be used that way, and thus wouldn't be used at the class level.
    SilverHorse : I just follow this habbit ...since if not doing so, my reharper pluggin wave me a suggestion.
    Jon Skeet : That note was written by one of the language designers (Eric Lippert). I think it's fair to assume that he believes it's one of the benefits - where the variable's context is visible. It's possible that without anonymous types it wouldn't have made it into C# 3.0...
    Jon Skeet : ... but that's no reason to not reap its benefits in other situations anyway. The bar is set high enough that the benefits of a feature have to be really significant to make it into the language - but benefits which may not be great enough on their own are still worth using when "free".
  • It's not as simple as implementing var in a method since you also have to take into acccount different modifiers and attributes like so:

    [MyAttribute()] protected internal readonly var list = new List<T>();
    

    What I would really have liked is a type-inferenced const!

    public const notFoundStatus = 404; // int
    
    Lasse V. Karlsen : The attributes and modifiers would have no impact on this as the var is just a placeholder for the type of the expression on the right side of the assignment operator. The compiler just magically substitutes the right type, and it could do that with modifiers just as easy.
  • Here's what Eric Lippert has to say. He's a senior developer at Microsoft.

    Why no var on fields

    From Brian

0 comments:

Post a Comment