Since the introduction of ‘var’ keyword in C# 3.0, lots of developers started using it very large scale mode. I have done code reviews of lots of projects since the introduction of ‘var’ keyword and the trend of ‘var’ keyword is in usage of lots projects, and one thing what I noticed is that, ‘var’ keyword has made developer’s life much easier.
If you are from a VB 6.0 background, usage of ‘var’ keyword was strictly prohibited only to certain places, developer should not get confused themselves with ‘var’ keyword from VB 6.0 or in JavaScript which both are totally unrelated.
The main purpose behind ‘var’ keyword in C#, is to use it when the type in unknown. For example, I have a method which returns CustomerDetails and it contains information such as Customer ID, CustomerID,CustomerCity,Zip etc. which has different data types.
Usage of ‘var’ keyword at everyplace makes code very difficult for other developers to understand. For example, a developer wants to check employee status with following code, by looking at following code anyone can understand what a developer wants to achieve.
Whereas in following code snippet, developer need to spend at least some additional time to understand the code.
In above example, developer can use known type ‘bool’ instead of ‘var’ which is very easy to understand.This is just one sample example, but imagine, if developer starts using 'var' keyword in entire project for the variable declaration.
One of the point which developer need to understand is that declaration of ‘var’ keyword makes your code incompatible .Net framework 2.0 and below.
It is recommended to use ‘var’ keyword if data type is unknown, you want to take advantage of LINQ or if your class name is big.
Other points related to ‘var’
• ‘var’ is an implicit declaration
• ‘var’ is defined statically
• Compiler define data type by looking at the right side value
• Data type are defined at compile time and not at runtime
If you are from a VB 6.0 background, usage of ‘var’ keyword was strictly prohibited only to certain places, developer should not get confused themselves with ‘var’ keyword from VB 6.0 or in JavaScript which both are totally unrelated.
The main purpose behind ‘var’ keyword in C#, is to use it when the type in unknown. For example, I have a method which returns CustomerDetails and it contains information such as Customer ID, CustomerID,CustomerCity,Zip etc. which has different data types.
Usage of ‘var’ keyword at everyplace makes code very difficult for other developers to understand. For example, a developer wants to check employee status with following code, by looking at following code anyone can understand what a developer wants to achieve.
Whereas in following code snippet, developer need to spend at least some additional time to understand the code.
In above example, developer can use known type ‘bool’ instead of ‘var’ which is very easy to understand.This is just one sample example, but imagine, if developer starts using 'var' keyword in entire project for the variable declaration.
One of the point which developer need to understand is that declaration of ‘var’ keyword makes your code incompatible .Net framework 2.0 and below.
It is recommended to use ‘var’ keyword if data type is unknown, you want to take advantage of LINQ or if your class name is big.
Other points related to ‘var’
• ‘var’ is an implicit declaration
• ‘var’ is defined statically
• Compiler define data type by looking at the right side value
• Data type are defined at compile time and not at runtime
Comments