Variables in C# : Var vs Type

When I first started coding I was taught JavaScript. When it came to setting up variables in JavaScript you had three options: var, const or let. And really nowadays that comes down to let (if the variable value will change) or const (if the developer doesn’t want the variable to be changed). But it seems pretty simple to understand.

But it got confusing when I started my first software developer job. You see, I jumping from JavaScript to C#. There’s a whole range of differences between those two languages which I’ll explain over a few posts.

So for this post I wanted to focus on variables. As I said, in JavaScript you had three. But in C# it got confusing. I was taught that in C# you had to use the type.

Yes, I was probably a lazy developer but it was confusing to me that I had to explicitly state the type of a variable when I was declaring.

For example, if I wanted to create a property for a person’s name. As this would be a string I would have to write:

string Name;

If I wanted to store someone’s age it would be:

int Age;

Those were the simple ones. Lists and arrays just went over my head for the first few months. I can’t tell you why, but it was difficult for me to get to grips with explicitly declaring variable types.

It wasn’t until I really got into studying in my spare time where I discovered you didn’t have to stick to that format. That actually, in recent versions of C# they had introduced “var”.

This meant that a developer could declare a variable with var, and the IDE would automatically figure it out. However there was a catch, the IDE isn’t a mind reader. Although you could get away with declaring a variable without initalising it when you were explicitly stating the variable type – if you wanted the IDE to figure it out so you could use var then you would need to initialize it while you were declaring it.

Using our examples of name and age these would turn into:

var Name = “Samantha”;
var Age = 15;

When I discovered var it felt like someone had been hiding this great secret from me. So I had to know: why were the developers who were teaching me not using it? So I asked around.

The answer was simple. By explicitly stating variable types it improved readability. So while it may seem more work to me, it made sense that some developers preferred it.

So which one should you use?

If it’s a work code base, or one you are working on with others, stick to whatever is used in the code base. Ideally you don’t want to be switching.

However if it is your own project go with the one you are most comfortable with. Personally I prefer var, and I now work for a company which leans more towards var so it works for me. But I think it is good to have an understanding of both options.

2 responses to “Variables in C# : Var vs Type”

  1. […] I have previously posted about stating a variable in C# (if not, you should definitely check it out here). Well let’s discuss the different types of variables that you may come across while […]

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: