In the Var declaration,
The compiler infers the type from the assigned value.
on
var a = 100;
var b = "test";
Why do we need them (why can't we use objects directly)
Because the object does not provide type security
object objtest = 1;
objtest = "test";
it works great.
But var provides type safety
var a = 100;
a= "test";
this does not compile and will give a compile-time error.
Where can we use them
- Linq queries returning anonymous types
- , ().
- .
.
RootClass rt = new RootClass ();
List<RootClass > rt = new List<RootClass >();
var aaaaaa = new RootClass ();
var ls = new List<RootClass>();