Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



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, 


Published Mar 21 2008, 10:19 AM by Derik Whittaker
Filed under: ,

Comments

James Gregory said:

Only if you have Option Strict off ;)

# March 21, 2008 3:09 PM

Stevi Deter said:

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.

# March 21, 2008 3:28 PM

Derik Whittaker said:

@Stevi,

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.

# March 21, 2008 4:29 PM

James Gregory said:

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.

# March 21, 2008 7:06 PM

Sergio Pereira said:

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

# March 21, 2008 9:34 PM

Derik Whittaker said:

@Sergio,

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'

# March 21, 2008 9:41 PM

Kyle Baley said:

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.

# March 21, 2008 11:03 PM

Derik Whittaker said:

@The Hillbilly,

So, this is how you got your nickname :)

# March 22, 2008 6:19 AM

Kyle Baley said:

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

# March 23, 2008 3:11 PM

Peter Ritchie said:

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>;

# March 25, 2008 9:28 AM

Derik Whittaker said:

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

# June 25, 2008 6:31 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News