Implicitly typed variables (var) C# with Example



Implicitly typed variables (var) C# with Example

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 set an int to an instance of StringBuilder 

0 Comment's

Comment Form

Submit Comment