Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 802
Matt Avatar asked Jul 27 '09 23:07

Matt


1 Answers

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>();
like image 53
Steven DeWitt Avatar answered Oct 08 '22 03:10

Steven DeWitt