-2

I hardly ever use the var variable type declaration in my C# code. Usually only when doing an odd HTML to ASP cast so, very rare.

But some programmers seem to always use var. Why would you do this?

C# is a strongly typed language. I would think that would make it bad-form to use a var declaration most of the time.

T.S.
  • 18,195
  • 11
  • 58
  • 78
TheJoe
  • 57
  • 10
  • 8
    I dont think you know what `var` is – maccettura Apr 25 '18 at 15:55
  • 3
    `var` is not equal to `dynamic`, it is an "implicitly typed field", so this is still *strongly typed* – bassfader Apr 25 '18 at 15:55
  • Say I have a method that turns MyBlueClass, and I assign that returned value to a variable. I can either use var, or MyBlueClass. In the future, for whatever reason, I change that same method to return MyRedClass. If I used var originally, there's now a lot less work that needs to be done. – p3tch Apr 25 '18 at 15:56
  • You really need to read an introduction to the `var` keyword – Camilo Terevinto Apr 25 '18 at 15:57
  • `var` is just short-hand for "let the compiler figure out the variable type from what is on the right". It is not dynamic or runtime determined. – Ron Beyer Apr 25 '18 at 15:57
  • 4
    To assist you on your learning journey, see [the documentation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var) – maccettura Apr 25 '18 at 15:57
  • 2
    Possible duplicate of [Use of var keyword in C#](https://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp) – maccettura Apr 25 '18 at 15:58
  • `var i = 1` - OK. `var i = myObject.Getsomething()` - not super ok because hard to read, what is `i` going to be? This should be your general guidance. And remember that real reason for `var` is anonymous types/Linq – T.S. Apr 25 '18 at 16:02
  • 1
    @T.S. - even that can draw differing opinions - is it more important *what the specific type of i is* or *that i is whatever you obtain from GetSomething*? Sometimes you care about specific types, sometimes you care more about the algorithms and so long as the code still compiles, you don't care if GetSomething changes from returning an int to returning a long. – Damien_The_Unbeliever Apr 25 '18 at 16:24
  • @Damien_The_Unbeliever I know what you're saying and I am familiar with this discussion. This is gray area. But man, how helpful `int i = myObject.Getsomething()` during code reviews. – T.S. Apr 25 '18 at 16:32

2 Answers2

1

Beware, varis still strongly typed, it is not the same as dynamic. The difference is that it is calculated implicitly instead of explicitly declaring it.

As most C# developers use Visual Studio, you can check the real type by hovering your mouse over the variable in case you need it.

The use of var is extended due to simplifying developing, as many times it is either obvious or too complex (several generic types, etc.)

Pinx0
  • 1,248
  • 17
  • 28
  • Best answer I have seen. – TheJoe Apr 25 '18 at 16:00
  • 1
    *"As most C# developers use Visual Studio, you can check the real type by hovering your mouse over the variable in case you need it."* And then all hell breaks out, when you do code reviews, either in TFS web-based tools or even in the studio comparison tools when hovering is not always produces the hint. – T.S. Apr 25 '18 at 16:09
  • @TheJoe If you like this answer, why don't you just accept it? – T.S. Apr 25 '18 at 16:20
0

The main purpose of the var keyword in c# is to shorten variable declaration. it's there for ease of use.

For example:

var myObj = new SuperLongClassNameHere();

because of the right hand side, you already know its type, it saves you from having to type out potentially long names twice.

var does not create a variant type, the type is simply inferred by the compiler. If it cannot be inferred, it will generate a compiler error

LaggKing
  • 202
  • 2
  • 9
  • 3
    no. This is not main reason. that would be **anonymous types**. – T.S. Apr 25 '18 at 15:59
  • Anonymous types shorten initialization. `var` is part of the declaration. – P.Brian.Mackey Apr 25 '18 at 16:07
  • @P.Brian.Mackey `var` can be used only with initialization, never declaration-only. And Anonymous types are there for not pre-declaring one every time and LINQ of course. – T.S. Apr 25 '18 at 16:13