0

I am not clear about this thing that why don't we always use var keyword instead of explicitly writing the name of the type if var keyword can automatically determines the datatype of the right side value of initialize statement?

Sweeper
  • 213,210
  • 22
  • 193
  • 313
Ankit Yadav
  • 27
  • 1
  • 6
  • 1
    We _do_ use `var` though... – Sweeper Jan 30 '20 at 08:19
  • 1
    using `var` is recommended practice. And lot many developers use `var` instead of actual datatype while initializing a variable. – Chetan Jan 30 '20 at 08:21
  • 2
    Some people say that it makes it harder to read the code because you don't know what the type is. Ideally, your variables and method names provide sufficient indication of this (without going into Hungarian Notation). – ProgrammingLlama Jan 30 '20 at 08:22
  • @Sweeper if we can use var instead then why int,char,string are not deprecating ?? – Ankit Yadav Jan 30 '20 at 08:22
  • 1
    What do you mean? `var` is syntatic sugar, it still compiles to `int`, `string`, etc. – ProgrammingLlama Jan 30 '20 at 08:22
  • 1
    @AnkitYadav because var doesn't replace int/char/string, the var will be determined compiletime which type it will be. So the compiler replaces it. It's syntactic sugar. (and it also enables having anonymous type variables) – Jeroen van Langen Jan 30 '20 at 08:23
  • See [SharpLab](https://sharplab.io/#v2:D4AQTAjAsAUCAMACEEB0AlArgOwC4EsBbAU1QGEB7QgB3wBtiAnAZSYDd8BjYgZwG5YsEAGZkYRGUQBvWIjmJZ8kcgAsiALIAKAJTTF8+foOI2AQ0aJTiALyIARAAtidOhTsCYx+fgBmiTVbWtqbaRgYynl7GKACcAdoeUYgAvmGGkXKpGQrZyiBqAHI6etkGaXIoSIH2ABLOru7liL7+gcGhpfIRSQax8YlRWT1RRlnJQA=) for the compiled->decompiled difference between using `var` and `string`. – ProgrammingLlama Jan 30 '20 at 08:24
  • @John one thing i came to know about var that we can't assign null to it. – Ankit Yadav Jan 30 '20 at 08:29
  • 1
    You can, you just need to type it first (although why you'd do this instead of just declaring the variable fully, I don't know): `var a = (string)null;`. Obviously it makes a lot more sense as `string a = null;` – ProgrammingLlama Jan 30 '20 at 08:31
  • 2
    You can use `var` whenever it's legal if you like to (can't declare a `var` property or field, for instance). People that prefer to use the explicit type can do so if they like to. What kind of answer do you expect from the community here? – C.Evenhuis Jan 30 '20 at 08:35
  • 1
    We do use `var` and you'll see it alot in any codebase using LINQ extensively. Why? Because LINQ introduced many new things and programmers rejoice at not having to be explicit with method-scope variable types when dealing with LINQ. They go hand-in-hand. Which isn't surprising as both were introduced in C# 3 and/or .NET Framework 3.5. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var and https://en.wikipedia.org/wiki/Language_Integrated_Query –  Jan 30 '20 at 08:40
  • "if we can use var instead then why int,char,string are not deprecating" - this type of remarks is exactly why I dislike the use of `var`... it's a lazy shortcut that confuses people, and it makes code less readable. Weird generated Linq IEnumerable results are the only case in which I consider using them. – Nyerguds Jan 30 '20 at 09:08
  • I am reopening this question because there are a few cases where `var` physically cannot be used, as outlined in my answer. – Sweeper Jan 30 '20 at 10:23

2 Answers2

4

var is often used by a lot of developers, including myself, but it does not completely replace writing out the type names.

Here are some situations where you can't use var:

  • Everywhere where you need a type but it's not a local variable declaration. This includes:
    • field types
    • method return types
    • method parameters
    • property types
    • casting
    • and many more...
  • When you try to program to interface

    IEnumerable<int> ints = new List<int> { 1, 2, 3 };
    
  • When the type can't be inferred. (as a consequence, you can't do var x = null; or just var x;)

  • When you are using a version earlier than C# 3.0, as this is a feature added in that version
  • When you need unsigned integers, as var x = 0; will always infer int, not uint.
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • ...and pre-[C# 3.0](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var) ;) –  Jan 30 '20 at 08:42
  • Another case that can't use `var`, which I encounter a lot when working with old file types: specifying the size and sign-capability of your integers. – Nyerguds Jan 30 '20 at 09:14
1

var is useful to store results of linq queries where you do not have any class defined that resembles the structure of the result of query. One of the primary reasons why var was created.