Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Use of 'var' in C#
Message
De
30/04/2010 13:14:42
 
 
À
30/04/2010 12:27:52
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01462533
Message ID:
01462543
Vues:
91
>var I think maybe short for variant. Not sure, but putting var before cutomer will ensure that any type returned will
>fit into the "customer" variable.

Not really. From :http://msdn.microsoft.com/en-us/library/bb384061.aspx :
"The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. "
and
"It is important to understand that the var keyword does not mean “variant” and does not indicate that the variable is loosely typed, or late-bound. It just means that the compiler determines and assigns the most appropriate type"

Only two reasone to use var really :
(a) with anonymous types (where you have no option)
or
(b) laziness :-}

>
>Such as:
>
>var varOne = new List<int>(); // will be of type IList
>
>var varTwo = new ENumerable<int>(); // will be of type IEnumberable
>
>You can also say:
>
>IEnumerable<int> someEnumType = new List<int>(); 
>
>But you can't say the reverse:
>
>IList<int> someListType = new Enumerable<int>();
>
>So var ensures that it will convert to the correct type.
>
>
>>What's the benefit of
>>var customer = new Customer();
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform