Search code examples
c#.netimplicit-typing

What are the benefits of implicit typing in C# 3.0 >+


The only advantage I can see to do:

var s = new ClassA();

over

ClassA s = new ClassA();

Is that later if you decide you want ClassB, you only have to change the RHS of the declaration.

I guess if you are enumerating through a collection you can also just to 'var' and then figure out the type later.

Is that it?? Is there some other huge benefit my feeble mind does not see?


Solution

  • It's mostly syntactic sugar. It's really your preference. Unless when using anonymous types, then using var is required. I prefer implicit typing wherever possible though, it really shines with LINQ.

    I find it redundant to type out a type twice.

    List<string> Foo = new List<string>();
    

    When I can easily just type var when it's obvious what the type is.

    var Foo = new List<string>();