0

Its been few months that I am using Razor as a view engine in my ASP.NET MVC applications and I am using variable like:

string myVariable = "My Variable String";

Or this:

int count = 0;

But today i find out that we should only declare variable with var keyword.

Variables are declared with the var keyword according to w3school and All the variable in official asp.net razor documentation, they are using var keyword in razor documentation example.

So my questions are:

  1. What is the benefit of using var keyword instead of explicitly specifying the type of the variable?

  2. As I told you, I am using string and int type variable, am I doing wrong?

Shahzad
  • 1,315
  • 2
  • 22
  • 42
  • There is nothing wrong with explicitly declaring the type (the `var` keyword just means that the compiler will infer the type) –  Feb 13 '17 at 08:25
  • Mate then why this point is in w3school **Main Razor Syntax Rules for C#** ??, I just don't understand the point. – Shahzad Feb 13 '17 at 08:28
  • 1
    Like a lot of the examples on `w3schools`, the statement is simply wrong. –  Feb 13 '17 at 08:30
  • What about the official documentation of razor from microsoft? as i provided the link, they are using var, not specifying any data type explicitly. – Shahzad Feb 13 '17 at 08:32
  • 1
    look, w3wchool is just a website, published by who knows who. That website is wrong more than it is correct, so do not take anything they say as something you should follow. – Andrei Dragotoniu Feb 13 '17 at 08:33
  • You can do either. No where is there any official documentation saying you must use `var` –  Feb 13 '17 at 08:33
  • @StephenMuecke can you please answer my questions as a answer to that question? also can you please include some reference for the razor that is authentic? – Shahzad Feb 13 '17 at 08:39
  • I did not understand the answer posted by @AndreiDragotoniu – Shahzad Feb 13 '17 at 08:39
  • @ShahzadMirza : elaborate please what you didn't understand. It implies you don't understand the `var` keyword at all. – Balázs Feb 13 '17 at 08:40
  • 1
    What did you not understand? (and you should be commenting on that answer if you need clarification) –  Feb 13 '17 at 08:41
  • we use var to declare implicit type variable those we don't know their type but what if i know the type of variable that is being used on view what should i prefer explicit data type or use var for compiler to identify the variable type? – Shahzad Feb 13 '17 at 08:44
  • @ShahzadMirza *we use var to declare implicit type variable those we don't know their type* - in a strongly typed language (leave dynamics alone for now), there is nothing like *we don't know the type* because everything has one exact type. Compiler-generated types, aka anonymous types are knows types as well, just they are generated by the compiler and for this reason you cannot directly refer to them with your code. This is why `var` was introduced in the first place. – Balázs Feb 13 '17 at 08:48
  • The question regarding when to use what is more like a preference-based thing but with keeping the points mentioned by @AndreiDragotoniu in mind. – Balázs Feb 13 '17 at 08:48
  • All in all, from a technical point of view they behave the same way because the compiler *will* resolve `var` into the actual type based on what the type of the value assigned to it is. So after compilation, there is *absolutely no difference between them* because the implicitness of the variable disappears. So it just boils down to the question what is easier to read and what is more telling to the reader. – Balázs Feb 13 '17 at 08:51
  • these are some satisfied answer to me thanks mate @Balázs and thanks everyone for this quick response. :) – Shahzad Feb 13 '17 at 08:55

1 Answers1

3

that's simply not true. You most definitely should not declare everything with the keyword var. A good rule of thumb is to use var to replace long types declarations when the type of the variable is clear from the declaration code.

Example :

do use var like this:

var someName = 0;
var someName = string.Empty;

var someList = new List<Mynamespace.MyType>();

Do not use var when the actual type is not clear:

var myVar = AMethodWhichReturnsSomeResult();

here the type returned by the method can change, meaning that you can have very subtle bugs when that happens.

So in short, be smart about this. use it to your own advantage but not when the type is unclear to the reader.

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32
  • what you mean by **Do not use var when the actual type is not clear** ?? – Shahzad Feb 13 '17 at 08:41
  • It means that, as in the provided example, just by looking at the code, you only see, "hey, here's a method call, oh, wait, what does it even return?". In other words, even descriptive method names rarely give you any idea what the type of the returned data is, especially when it is not a primitive but a complex type. – Balázs Feb 13 '17 at 08:42
  • 1
    @ShahzadMirza, One of the most common questions of this site is [The model item passed into the dictionary is of type .. but this dictionary requires a model item of type](http://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) and the most common cause is because of the use of `var` (and had those users explicitly declared the type, the error would not occur) –  Feb 13 '17 at 08:43
  • thanks @StephenMuecke this link is very helpful to understand the idea :) – Shahzad Feb 13 '17 at 08:57