Lots of developers confuse the var keyword in C# with JavaScript.
The var keyword was introduced in C# 3.0, and it is basically implicitly defined but is statically defined, var are resolved at compile time not at run-time.

The confusion is there also a var keyword in JavaScript, but in JavaScript var is dynamically defined.

Example:

In C# this would be illegal

while in Javascript this will be ok, since it is dynamically typed.

In C# one place that you are required to use var is when using Anonymous Types, that is when the compiler generates the internal type.

Example:

One can also use var when using LINQ

The above statement returns us a List of string names, the downside to this is if someone is reading this code they may think its a list of Animals that is returned, one can also define the return type in the foreach statement to make it clear or in LINQ. This may create a more readable code but I think having var also does the job well once you get comfortable using it.

Another place that var comes in handy is when defining a type to avoid repetition.

In conclusion:

  1. Javascript var != C# var
  2. Anonysmous Type requires var
  3. var can be used in LINQ return type
  4. Use var to avoid duplicating declaration names of type