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.
![]()
4 Responses to var Keyword In C#
Leave a Reply Cancel reply
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Free Icons And Images With Visual Studio 2008
- Capture XML In WCF Service
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML


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…
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.
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.
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.