software master at the intersection of technology, science and art

home

download

implicitly typed


Local variables can be given an inferred "type" of var instead of an explicit type. The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The inferred type may be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library.


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.


    The var keyword may be used in the following contexts:
  • * On local variables (variables declared at method scope).
  • * In a for initialization statement.
  • * In a foreach initialization statement.
  • * In a using statement.