Var is an implicit data type. It means var can represent any data type that can represent at compile time. So it means var does compile time type casting.
Lets look on some code :
So now we can easily see that from the above code we can not implicitly convert string to int. If we check this with ILDASM tool then we will not see any var keyword in IL code. .Net will treat this as Int variable.
Some more point to notice about var :
1. We can not assign null to var keyword.
2. We can not pass var as a parameter of the method.
3. We can not define return type as var in our method.
4. There is no performance issue with var as .Net execution engine doesn't bother about Var. It will treat it as int or the type we defined at compile time.
Var mystring = "mystring"; will define mystring as string variable.
Happy Coding Hours
Lets look on some code :
So now we can easily see that from the above code we can not implicitly convert string to int. If we check this with ILDASM tool then we will not see any var keyword in IL code. .Net will treat this as Int variable.
Some more point to notice about var :
1. We can not assign null to var keyword.
2. We can not pass var as a parameter of the method.
3. We can not define return type as var in our method.
4. There is no performance issue with var as .Net execution engine doesn't bother about Var. It will treat it as int or the type we defined at compile time.
Var mystring = "mystring"; will define mystring as string variable.
Happy Coding Hours