C# Implicitly Typed Local Variables and Arrays

As a whole C# is a strongly-typed language. In other word the C# variables can hold explicitly defined data type.

From C# version 3.0 on the developer get the possibility to assign any values to a variable and the compiler to obtain accordingly the data type. It is not in the C# style as we know it but has some advantages of course.

var intVariable = 1235;
var stringVariable = "Hello";

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 may be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library.

The biggest advantage of this new feature is the usage in LINQ query expression using anonymous data types. The anonymous type name is automatically generated by the compiler and is not available at the source code level, but his usage saves much developer's efforts.

For more information in MSDN click here

No comments:

Post a Comment