Programming, Investing and FI

Coding Standards – Var Keyword in C#

This post is about programming standards which is a more advanced topic, if you do not feel comfortable, you can continue the tutorials.

What is the Var keyword in C#?

The Var keyword is used to let the compiler infer what the type of the variable is without you having to do that thinking. With some knowledge of the language you will know that certain expression will output a certain type.

Meanwhile, it still requires you to do a bit more processing in your head to figure out what the type will be when you are reading the code after. In other words, writing will be easier, reading becomes a bit more complicated.

Please refer to the MSDN Website for more on the subject.

Why I am not that fussy of Var

I prefer reading a type

I find that it goes against the fact that C# is a strongly typed language even if ultimately it stays strongly typed. From my point of view it’s also easier to see the type of a variable because it appears first but when Var is used, you find yourself sometimes searching for the type of the variable.

Using Var

var Example = "Hello everyone!";

Not using Var

string Example = "Hello everyone!";

The preceding example can give you a better idea of the difference between using var and not using var. As you can see, you don’t even need to read the complete line to know that Example is of type String. That’s what I want to point out! Faster reading.

Keep the stuff organized

In my opinion, keeping track of the type of your variables makes sure you stay organized in your head. Variable types are straight, in line and not cloudy. If the type is something, it stays that way which prevents people from assigning other things to a variable. I am talking widely about the subject and am referring to languages like PHP where you can pretty much assign a number and then a string to a variable without anyone warned. You cannot do that with var but it might give this impression of looseness.

It’s all or nothing

I’ve seen a couple of cases where half of the variables would be declared as var and the other half with hard written types, not a good idea! If you really want to use var, I would say, make a decision and stick with it. In my case I choose not to use them, so I (almost) never use them.

I admit that dynamic variables can be handy

In some cases it can be very handy (even mandatory) to be able to declare dynamic types on the fly. But I still think that defining classes is a cleaner and a better way to stay organized in your head while programming.

The Bottom Line

You decide, but I would say either only var (when possible) or only types, it’s easier to read when it’s standardized, also I still believe that writing the type itself is cleaner. Sometimes you don’t have much choice, I am talking about anonymous types. For a last word on the subject, “[…] the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required.”


Next article Visual Basic/C# Tutorial 22 - Dealing with multiple forms
Previous article Software Design - Utility Classes

Related posts