Possible Duplicate:
Use of var keyword in C#
Which one is better? using var in variable type declaration or using true type like int , … ?
Why?
Which one is better?
public class A
{
var v1 = 0;
var v2 = string.Empty;
}
or
public class B
{
int v1 = 0;
string v2 = string.Empty;
}
As gdoron says, there’s no right or wrong answer, but in the interests of opinion, I’ll give mine.
I generally always explicitly use the type, rather than
varbecause I find it is easier to understand what type something is. E.g, for someone else reading your code, it’s clear what the type is.In your example, it’s hardly noticeable and a casual reader can easily see what is a string and what is an int. However, consider:
To the eyes, it’s not clear what the type of
fis immediately.That said,
varcan be useful when doing something like:as that surely saves some typing and it’s clear what the type is.