Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using implicitly typed local variables [duplicate]

I just installed a trial version of ReSharper and one of the first things I noticed is that it always suggests to replace explicitly typed local variables with implicitly typed ones, e.g:

public string SomeMethod(int aParam) {     int aNumber = SomeOtherMethod(aParam);     // should be changed to:     var aNumber = SomeOtherMethod(aParam); } 

I think explicitly typed variables are more readable (more explicit).

What do you think about ReSharper's suggestion? Is there any advantage in using implicitly typed variables? When do you use implicit/explict vars?

like image 894
M4N Avatar asked Mar 16 '09 15:03

M4N


People also ask

What is implicitly typed local variable?

Implicitly typed variables are those variables which are declared without specifying the . NET type explicitly. In implicitly typed variable, the type of the variable is automatically deduced at compile time by the compiler from the value used to initialize the variable.

Can a implicitly typed variable be set to NULL?

Because compiler cannot predict the type of null. Null can be assigned to any nullable datatype also to any reference type variable. So for implicit conversion, you have to cast null to some specific type.

Why should I use var in C#?

It is recommended to use var only when it is necessary, that is, when the variable will be used to store an anonymous type or a collection of anonymous types. The complaint that var reduces readability is not shared by everyone.

Which of the following keyword is used to declare a variable whose type will be automatically determined by the compiler?

The auto keyword directs the compiler to use the initialization expression of a declared variable, or lambda expression parameter, to deduce its type.


2 Answers

I personally only use “var” when I can clearly distinguish the variable Type by just reading the declaration, for example:

var someVariable = new List<int>(); 

In the example above, its evident that “var” refers to “List<int>”.

I don’t like to use “var” when I have to go to some method definition to find out what variable type “var” represents or by having to rely on visual studio intelli-popup or whatever that is called, for example this in not ok to me:

var someVaraible = SomeMethod(); 

I mean, what is the “SomeMethod” function supposed to return? Can you tell just by looking at the line of code? No you can’t, so that is why I avoid using “var” on those situations.

like image 149
Rene Avatar answered Oct 12 '22 04:10

Rene


There's a lot of discussion about this, but I think it all comes down to personal taste, just like using the 'this' keyword almost everywhere.

I personally prefer explictly typed variables, but when using nested generic collections things can become more readable using an implicitly typed variable. Look at:

Dictionary<string, Dictionary<string, string>> myDictionary = new Dictionary<string, Dictionary<string, string>>(); 

vs:

var myDictionary = new Dictionary<string, Dictionary<string, string>>(); 

EDIT: this SO topic covers the same topic, with some nice replies: What to use: var or object name type?

EDIT2: Working a lot with async nowadays, I find that using explicity typed variables can sometimes prevent nasty bugs. Consider this silly example where you would want to return the Id of a user. Also consider that GetUserAsync returns a Task<User>. If you use implicitly typed variables, you would end up using something like this:

public long GetUserId() {   var user = GetUserAsync();   return user.Id; } 

This compiles, but it is wrong. 'user' is actually a Task<User>. And it compiles as Task also has an Id property. In this case, one would accidentally return the Id of a Task instead of the User.

public long GetUserId() {   User user = GetUserAsync();   return user.Id; } 

The above does not compile, as the compiler will complain that you cannot cast a Task to a User. Adding the await keyword of course solves this.

I've actually had this happen to me once :-)

like image 39
Razzie Avatar answered Oct 12 '22 03:10

Razzie