When should var be used in C #?

Possible duplicate:
What is the var keyword point?

What is the type of var and should it be used in C #? When should it be used?

Can you specify the type var for the parameters for the method?

+4
source share
7 answers

var is not a type. This is a convenient short manual recording that saves you a lot of typing. The following two declarations are equivalent:

 Dictionary<string,List<int>> dic = new Dictionary<string,List<int>>(); 

and

 var dic = new Dictionary<string,List<int>>(); 

As you can see, this makes typing and reading easier. And if you want to change the data type, you only need to change the right part.

But where I really like the most, in the foreach loops:

 foreach ( var item in someList ) 

Here, the element automatically takes the correct type, depending on the type of someList, and it allows you to change the type of someList without changing the element's declaration.

In short, var is GREAT and should be used.

EDIT : No, you cannot specify var as a parameter to a function. I think the closest thing you can get is generics. And I also completely forgot to mention anonymous types. With them you should use var.

+6
source

This is part of the language, so yes, it should be used. However, this is not a type, but means a placeholder for the actual type defined by the compiler.

When do you use it? This has been asked before, many times:

+4
source

var not a type, it is an instruction to the compiler so that it can determine the correct type to use.

This means that there is no difference in the output of the following two lines:

 var s = "Test"; string s = "Test"; 

As for when to use it, you will have to come up with your own rules. There are very few things you can do with var that you cannot do with a specific type name, but there is one thing: anonymous types.

You can do:

 var x = new { A = 10 }; 

but you cannot come up with a strong type for this, because it does not have a type known at compile time, instead the compiler will create the type at compile time, and then var will use that type.

The fact that var was created to support that it can also be used without anonymous types is a bonus, but you will have to make your own rules when to use it in these cases.

Personally, I use it only when the type is easily read from the source code, so I would use it for:

 var dict = new Dictionary<int, string>(); var stream = new FileStream(...); 

but not for:

 var dict = GetDataStructure(); var stream = OpenFileForReading(); 
+3
source

We tend to use it when interacting with all our domestic libraries.

This is due to the fact that we often change things as libraries evolve, and quite often changing a type name or return type does not actually violate what it uses.

We also use it most of the time with the .NET platform.

However, we made a direct ban on its use with such native types as int, string, bool, etc., because it is not always clear what type of variable is in these cases.

+2
source

Well, as mentioned earlier, var is a short note for the type name of any variable. For me, I find that var is used by many developers, and I think that var makes the code less readable. But one case where u should use it is definitely an anonymous type variable.

0
source

Just to add to another comment, I suggest using var only when you can infer the type by simply reading the code

 var x = new Widget(); //ok var y = Customer.Create(); // ok var z = DoSomething(); //not ok 
0
source

Some answers introduce Anonymous types .

var keyword in MSDN.

Be aware that var not a type, but a placeholder and can only be used within methods or properties (definition of a local variable). You cannot use it in the definition of a local member (private members of a class), and you cannot define a method that should return any type as generics , where you can use any name other than var (or any other reserved keyword), eg:

 public IList<var> GetItems() { // Here the compiler will complain about the context of using `var` keyword within a local variable definition only. } public IList<T> GetItems() { // Here, we're talking about generics. } 

So, here are the basic rules to use with the var keyword:

  • Only local definition of variables;
  • Use var with anonymous types (e.g. Linq query);
  • Not to be confused with generic types;
0
source

Source: https://habr.com/ru/post/1309903/


All Articles