var keyword

Var role in C # with C ++, the auto, the type automatically infer local variable by a compiler can reduce the code, for example:
VeryComplicatedType X = new new VeryComplicatedType ();
not good code, as VeryComplicatedType rewritable twice, above the sentence should be changed to:
var = the X-new new VeryComplicatedType ();
appropriate use var, not only can make the code look cleaner, and sometimes can also increase the flexibility of the code, third-party libraries such as data type is changed, it is possible to own code You do not need to synchronize changes.

var only two purposes:
  • Var can be used when typing less declare variables, especially in the generic type declaration of a nested variable.
  • When referring to objects stored anonymous type must be used, because they can not know in advance the type name, for example: var foo = new {Bar = "bar"};

var only local variable type may be used, can not be declared var domain, attribute, parameter or return type.

----------------
Disclaimer: This article is CSDN blogger "2222345345" of the original article, follow the CC 4.0 BY-SA copyright agreements.
Original link: https: //blog.csdn .net / ftell / article / details / 81839386

Guess you like

Origin www.cnblogs.com/weilei/p/11498409.html