C# var

var keyword indicates that the variable type is not declared during definition, but can be decided by the compiler.

//Common variable definition
int i=3; //type is int
string s="csharp"; //type is string
var i=3; //type is unknown, but compiler can infer its type is int
var s="csharp";

var keyword defined variable must be initialized during definition. The type of the variable can then be decided based on the value. After definition, the variable type can not be changed.
var s; //error
var s = "csharp";
s = 3; //error

var defined variable must be a local variable.

endmemo.com © 2020  Terms of Use | Home