.Net Programming - Financial independence

What Is the Var Keyword in C#?

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

Here is an example of an “on the fly” declaration of an anonymous type containing only the Age of the clients using the var keyword. Let’s say I have a Client class that has two properties, Name and Age.

List clients = new List();
clients.Add(new Client() { Name = "Alex", Age = 20 });

//Creating an object on the fly and assigning an IEnumerable of that object to Results
var Results = from client in clients
              where client.Name == "Alex"
              select new { Age = client.Age };

foreach (var currentResult in Results)

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 How to Handle Multiple Windows Forms With Visual Basic/C#
Previous article What Are Utility Classes (And Why I Hate Them)

Related posts


No Comments Yet!

You can be first to comment this post!

Leave a Comment

Your data will be safe! Your e-mail address will not be published. Also other data will not be shared with third person. Required fields marked as *