|
var Keyword In C#
I have a confession to make. When I saw var keyword for the first time I did not like it at all. My first opinion was that var was so variant like. Call me a control freak but I like to clearly see what I am declaring. And var was something I thought made the code less readable. However since I started working with LINQ I got into a habit of using var. I realised that it makes my life easy by letting the compiler figure things out for me. So in this post I will pay due respect to var keyword.
Anything declared using var keyword is implicitly typed which means that compiler will determine the correct type based on what the variable is initialised to. Here I am declaring three variables x, y and z. All of them are initialised to different values which are also of different Types such as DateTime, string and List<T>.
var x = DateTime.Now;
var y = "Hello";
var z = new List<String>();
Let’s examine the IL to verify that x, y and z are of Types they have been initialised to.
Here you can see that x is of valuetype, y is a string and z is of type List<string>.
I find this as neat little syntax sugar to which I have become addicted.
Keep in mind that you cannot write a statement like this.
var x;
This will give an error while compiling. And it makes sense because the compiler does not know which Type it should consider x as.
![]()
Get Updates By Email
Popular Post
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Free Icons And Images With Visual Studio 2008
- Windows 7 Control Panel In Classic Mode
- Dynamic Sort With LINQ
- Use SqlConnection With LINQ To SQL
- StyleCop Tutorial
- Write To Vista Event Log Using C#
- More Details Emerge On Microsoft Master Certification
Tag Cloud
Code Snippets
- Get Current Windows User In C#
- Get Width And Height Of Image In C#
- Get Windows Registry Size With WMI And C#
- Reverse Array Elements Using C#
- Convert Hexadecimal To Number In C#
- Get Free Disk Space Using T-SQL
- SQL Server 2008 – Get All Indexes In A Database
- Get Name Of Current Executing Assembly In C#
- Get CD Or DVD Drive Information Using WMI And C#
- Get Last Row From Table Using LINQ To SQL


February 20th, 2009 at 6:57 am
But really…what’s the point of using var on something of known type? Like you said, var is specialized for dynamic frameworks and languages like Lambda and LINQ, where you don’t know what’s coming back.
When you do something like:
var myName = “Json”; //what tha hell is the point of this?
It DOES make code hard to read when var is applied out of context.
There is no logical reason not to put:
string myName = “Json”;
You don’t buy anything beneficial from using var for KNOWN data types. Because on the right side of the equal sign guess what, you KNOW the data type. It’s silly…
February 20th, 2009 at 9:16 am
Jason,
I could not agree more with you. You have to use var judiciously. It adds most value when working with anonymous types. I do not have problems using var once in a while but I stay away from using it for every declaration.
December 11th, 2009 at 3:44 am
In my experience “var” makes code more readable. I find it quite obvious to get the type. The only dubious use of “var” to me is potentially the return type of a method call where it isn’t clear what the method is returning. Still a well written method name can help a lot (without adding the return type to the name).
If I see var message = GetMessage(); I would generally infer a string.
I am a big believer in preferring readable code over code comments but I find overly verbose code less readable. The less cluttered the code the more readable it is to me.
And I have to look at a lot of other peoples’ code in code review.
In my industry we do a lot of code review and I am very pleased when people use var I do not find it in anyway a hindrance.
December 11th, 2009 at 8:32 am
Hi JohnG,
Thanks for your comment. I am also a believer of readable code and I find var to be elegant in certain situations. Of course a balance has to be provided.