J.P.Cummins

February 1, 2010

Is the C# var keyword considered harmful?

Until very recently, all my projects at work were written in pure unmanaged C++. Now that we're starting sprinkle a little C# in the mix, some interesting issues have arisen.

Today's issue revolves around the use of the var keyword. It's a relatively new concept in C# and is a topic of great debate. Basically, var allows for local type inference when the compiler can determine what the variable type should be.

Beginning in Visual C# 3.0, variables that are declared at method scope can
have an implicit type var. An implicitly typed local variable is strongly
typed just as if you had declared the type yourself, but the compiler
determines the type.

var - C# Reference

Thus, the following two declarations of i are equivalent:

var i = 10; // implicitly typed
int i = 10; // explicitly typed

The var keyword is great because it solves one of my pet-peeves with C# and Java -- having to write type names twice:

MyType foo = new MyType();

This is verbose and unnecessary. var optimizes for the lazy by reducing the number of characters that have to be typed. The fewer the characters, the shorter the line, the easier it is to read. See for yourself:

MyCustomTypeWithAVeryLongName foo = new MyCustomTypeWithAVeryLongName();
var foo = new MyCustomTypeWithAVeryLongName();

The debate still rages on and I doubt anyone will cave in until we come up with some coding guidelines. In the meantime, I will continue to propagate var throughout our codebase.

Posted by J.P. Cummins ● Comments (4)

Comments

Benjamin Feb. 2, 2010 at 5:32 a.m.

JP, I have to say I disagree, I understand the annoyance with typing the variable type twice, but I feel like the var keyword in C# is just MS attempting to make C# behave more like a loosely typed language without actually making one, as such I feel it pollutes the language. Anyways I'm not the biggest C# fan anyways so take it how you want.

Larry Feb. 2, 2010 at 6:13 a.m.

I love the keyword var. It saves me tons of keystrokes and it makes my code easier to debug.

Chad Feb. 2, 2010 at 7:48 a.m.

JP, I utilize the var keyword differently based on the type of development that I'm doing. If I'm hacking out a prototype or sample application, I use var because it easier to change the type of my variable - usually named "foo".

However, when writing production code, I only use var for anonymous types. I realize that this is opinion, but I actually find that explicitly declaring the type of my variable is less typing and more readable.

It's more readable because I only have to read two words, the type and the variable name. I don't have to look past the new keyword or search for a "(" to ignore contructor arguments.

It's less (or at least equal) typing because Intellisense gives me the list of constructor overloads after typing the new keyword if it already knows the type. Three letters + a Ctrl+J will usually give me my type for the type declaration too.

Nice post - Welcome to C#!

J.P. Cummins Feb. 2, 2010 at 10:58 a.m.

@ben - Don't hate on var or on C#. It's actually a quite powerful keyword and language.

One thing I forgot to mention in the post, is that var enables you to dynamically create new types without having to explicitly declare the interface. Check this out:

var result =
from s in aBunchOfWords
where s.Length == 5
//Creates a new anonymous type with name/value pairs
select new {Value=s, Length=s.Length, FirstThreeLetters=s.Substring(0,3)};

//Print values
foreach (var x in result)
Console.WriteLine("Value: {0}, Length:{1}, SubString:{2}",
x.Value, x.Length, x.FirstThreeLetters);

Cool, huh? You didn't have to create your own type for x. Without var, you'd have to write:

public class MyType
{
public string Value;
public int Length;
public string FirstThreeLetters;
}

I believe the technical term for this feature is called "variable projection" and is used heavily with Linq.

Also, regarding dynamic typing - C# 4.0 will actually support it.
http://msdn.microsoft.com/en-us/libra...

Commenting is disabled after one week.

My name is J.P. Cummins and I live in Seattle. This is my personal website.