2

I am noticing more and more that our developers are using the keyword var in preference to actually casting the type, as an example

var check = context.ALLProducts.Any();

Obviously Any() is only ever going to return a boolean, so as far as I am concerned it should be set to a boolean. I've also seen examples where var is used on functions that return Int or string or any number of types that I don't think should be var.

So my question is - is there any real reason I should have an issue with var being used for everything, other than code readability and supportability? I'd like to reinforce my argument if possible other than saying I don't like it.

Andrew
  • 2,315
  • 3
  • 27
  • 42
  • 3
    http://www.infoq.com/news/2008/05/CSharp-var – majjam Nov 26 '13 at 09:54
  • Thanks, that's very interesting. I realise it is very subjective I was just hoping someone would come back with a concrete reason for not using var for everything. – Andrew Nov 26 '13 at 09:59
  • 1
    @Andrew var is just syntactic sugar, so there will not be a concrete reason (i.e. there won't be a situation where a var'ed variable will behave incorrectly compared to the explicitly typed variable). Therefore, this is purely a style thing. – RB. Nov 26 '13 at 10:01
  • I haven't actually tried this, but what if you had var result = a / b and one set of numbers gave you a double result and the next a whole number - how would the compiler cope? – Andrew Nov 26 '13 at 10:04
  • 3
    possible duplicate of [Use of var keyword in C#](http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp) – Rui Jarimba Nov 26 '13 at 10:19
  • @Andrew It will cope by using the return type of the divide operator you chose. This will be based on the datatype of a and b (e.g. if they are both integers, then result will also be an integer, as per [the documentation](http://msdn.microsoft.com/en-us/library/vstudio/3b1ff23f.aspx)). – RB. Nov 26 '13 at 10:28

2 Answers2

2

Not sure if this was covered in the links, but a positive for using the var keyword is for situations where you need to refactor models.

Take an instance where a function needs to change the return value type, but the new type shares many, if not all, of the properties as the previous type. If you used var, you'll have little refactoring, if any.

A real-world example might be a developer returning a dto from a repository and not translating it to a viewmodel before binding. If you used var for your model and set it to the translated viewmodel, you probably will have little/no refactoring in your action to adjust for the changed type.

ohiodoug
  • 1,493
  • 1
  • 9
  • 12
1

That code that you've posted is the exact same as using bool

var check = context.ALLProducts.Any();

You simply won't be able to put a non-bool into check if you declare it like that.

as an example, this program will give you a compiler error;

class Program
{
    public static void Main()
    {
        var check = true;
        check = 1;
    }
}

Constant value '1' cannot be converted to a 'bool'


The advantages are it takes less typing, especially if your type is

System.Namespace.ReallyReallyLongTypeName thing = SomeFactory.GetObject();

and, of course, the disadvantage is that you can't just glance up at the code and immediately know what type it is.