Saturday, November 15, 2008

Type Inference in C# using var keyword

Implicitly Typed Local Variables in C#

Instead of explicitly declaring the variable of a particular type (int, short, string etc), one can define a variable of type ‘var’ and allowing the compiler to infer the type

For example,

var i = 10;

is equivalent to

int i = 10;

The inferred type may be a built-in type, an anonymous type, a user-defined type, a type defined in the .NET Framework class library, or any expression.

Implicit Typed Variables in .NET, .NET Variant, Var keyword in .NET,

No comments:

Post a Comment