Derik Whittaker



Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at
Is var the new Dim?

In C# .Net 3.5 they added the var keyword.  The intent the var keyword is to allow the object type to be implicitly inferred from the expression used to initialize the variable.  However, more often then not I hear people using it because it saves them time typing. 

So, if the main reason SOME people are using var i have to ask the one simple qusetion

Is var any different then Dim?  I mean they both have 3 letters.  They both are used to declare variables........ Hum

I know that var is NOT the same as dim in the 'CLR' sense, so please do NOT try to explain to me how they are different

Till next time, 

Posted 03-21-2008 10:19 AM by Derik Whittaker
Filed under: ,



James Gregory wrote re: Is var the new Dim?
on 03-21-2008 3:09 PM

Only if you have Option Strict off ;)

Stevi Deter wrote re: Is var the new Dim?
on 03-21-2008 3:28 PM

It really boggles my mind that people would seriously state they base their coding decisions around saving keystrokes and no other good reason.

Mostly, I fear having to maintain their code.

I have RSI and I can still find the wherewithall type enough characters in my IDE to get it to pop up the type I really want.

Derik Whittaker wrote re: Is var the new Dim?
on 03-21-2008 4:29 PM


Not sure why you find it so strange that developers, the laziest people on the planet would do something simply because it saves them a few keystrokes.

James Gregory wrote re: Is var the new Dim?
on 03-21-2008 7:06 PM

I think the saved keystrokes will be much appreciated when using nested generics. Dictionary<string, List<int>> d = new Dictionary<string, List<int>>() is just noise.

Sergio Pereira wrote re: Is var the new Dim?
on 03-21-2008 9:34 PM

I think there are some important differences in the way VB programmers use Dim and they way C# folks will be using var.

In VB when you use Dim you cannot omit the type name (unless option strict off, as noted by James), in C# you can declare the variable without writing the type name anywhere.

With Dim it's possible to create non-initialized variables, with var it's not (you can initialize with a function that returns null, but that won't cause a compiler warning for example).

Derik Whittaker wrote re: Is var the new Dim?
on 03-21-2008 9:41 PM


You took the post way, way, way to serious.

The whole point (joke if you will) of the post was that people for a long time have knocked VB because variables were declared with the Dim keyword.  Now C# allows you to declare a variable with var.

They both are 3 letters long.... I just having fun with this post at the expense of the 'Hillbilly'

Kyle Baley wrote re: Is var the new Dim?
on 03-21-2008 11:03 PM

Moq if you will. One less thing for me to worry about if I don't have to think about what my variable is going to do. Heck, I'm thinking I may just declare half a dozen placeholders at the beginning of each method 'just in case'. Names a,b,c,d,e, and f.

Derik Whittaker wrote re: Is var the new Dim?
on 03-22-2008 6:19 AM

@The Hillbilly,

So, this is how you got your nickname :)

Kyle Baley wrote re: Is var the new Dim?
on 03-23-2008 3:11 PM

You better believe it. In my family, placeholders are just another name for 'cousins'.

Peter Ritchie wrote re: Is var the new Dim?
on 03-25-2008 9:28 AM

With tongue firmly in cheek:

If it were about keystrokes, why "var" at all?

myVariable = 10;

...the compiler knows it has never encountered myVariable before, so it can "declare" it.  Since we're assigning an int to it, it  can declare it as Int32...

If "var" is just to differentiate a declaration from an assignment, we haven't gained much.  

If keystrokes were such a problem, we'd see lots of

using slst = System.Collections.Generic.List<string>;

Derik Whittaker wrote The var war is brewing
on 06-25-2008 6:31 AM

When C# 3.0/.Net 3.5 was released one of the new features that was included was the &#39;var&#39; keyword

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)