Implicitly typed variables var

suggest change

The var keyword allows a programmer to implicitly type a variable at compile time. var declarations have the same type as explicitly declared variables.

var squaredNumber = 10 * 10;
var squaredNumberDouble = 10.0 * 10.0;
var builder = new StringBuilder();
var anonymousObject = new
{ 
    One = squaredNumber,
    Two = squaredNumberDouble,
    Three = builder
};

The types of the above variables are int, double, StringBuilder, and an anonymous type respectively.

It is important to note that a var variable is not dynamically typed. squaredNumber = builder is not valid since you are trying to assign a StringBuilder to an int.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents