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:
If I wanted to store someone’s age it would be:
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.