The var keyword in C # 3.0

Possible duplicate:
What is the var keyword point?

Hello everybody,

I want to confirm the correctness of my understanding. If I do not use LINQ, then the only advantage of using var is to make it concise? Is this the correct understanding?

+2
source share
9 answers

No, you can use var to create anonymous types , regardless of whether you use LINQ:

 var anon = new { Name = "Anonymous", Age = 42 }; 
+17
source

It is also easier to work with these types. When you have very long generic types, the type name may interfere with visually identifying the variable name as part of the declaration.

 Dictionary<string, Dictionary<int, ICollection<object>>> 

especially if you come back and change it to

 Dictionary<string, IDictionary<int, ICollection<object>>> 
+4
source

From msdn:

Starting with Visual C # 3.0, variables declared as part of a method may have an implicit var type. The implicitly typed local variable is strictly typed, as if you declared the type yourself, but the compiler determines the type. after two declarations I am functionally equivalent:

 var i = 10; // implicitly typed int i = 10; //explicitly typed 

MSDN link here

+3
source

If you are not using LINQ, var allows you to declare a variable type once, not twice.

Example

 var myObject = new MyObject(); 

against

 MyObject myObject = new MyObject(); 

This can only be done locally, and is also useful for declaring anonymous types.

Example

 var myAnon = new { Name = "Something", Count = 45 }; 
+2
source

In addition to LINQ queries, I would very carefully use the var keyword. There is a specific instance where you just need an anonymous type, but it's a bit far, I think. Var can lead to very confusing code, because you have no idea what type you encounter when reading code, unless you use the intellisense crutch.

It bothers me more and more that I see so many snippets and snippets of code that do the following: this is lazy, not what the var keyword was intended to do:

 // Not too bad but still shouldn't be done because the only gain you have is keystrokes var Something = new SomeObject(); // Type here is not obvious, are you getting an int, double, custom object back??? var Something = GetLengthOfSpaghettiCode(); 

So use it for LINQ ... use it for anonymous types (if you use anonymous types outside of LINQ, you should carefully study why you need to).

A quote from MSDN (last line of the article) regarding the use of var:

However, using var has at least the potential to make your code more difficult for other developers to understand. For this reason, C # documentation usually uses var only when necessary.

Do not use it as a short cut to save keystrokes, the next guy looking at your code will appreciate it.

+2
source

To a large extent, yes. var can be used wherever the compiler can infer the type of a variable from any value that you assign to it. (Type inference rules are pretty complicated, so you can read the C # spec for a complete understanding.)

It is not entirely true that the var keyword is required to define anonymous types. For instance:

 var foo = new { abc = 1, def = 2 }; 

which can be used outside of LINQ queries, as well as internally, of course.

+1
source

I do not think that using var should be a problem - and I prefer it precisely for the purpose of reading code. First of all, var is only syntactic sugar and simply compiles to the proper type when IL is emitted. Regarding code readability, it makes sense to focus on the purpose for which the variable is used, and how it is assigned, and not just its type. The VS.NET editor shows the type in the line following it, anyway - if you just hover over it. So this should not be a problem. And as for debugging - if you see the Autos / Local / Watch windows - they display the types of all members.

I like to see code like this:

 var customers = GetCustomerList(); foreach (var customer in customers) { customer.ProcessOrders(); } 

Unlike

 List<CustomerObjectDeserializedFromWebService> customers = GetCustomers(); foreach (CustomerObjectDeserializedFromWebService customer in customers) { customer.ProcessOrders(); } 

var has its fairness, limited to using variables in local declarations that are also initialized during declaration. And in this case, if you omit the actual type, it definitely improves the readability of IMO.

EDIT: And for my part, it would be unfair not to warn about use, as shown below:

 var x = 20; 

This is not good; when a literal is applicable to several types, you need to know the default data type and therefore understand what is output for type x. Yes, by all means, I would avoid such statements.

+1
source

I believe that it is also used in WCF (Windows Communication Foundation) when working with data received through web services, etc.

0
source

I also found that using var also facilitates refactoring in low-grade projects. This is because we tend to have strong type variables, but usually the following code expects weaker types. Using var, you compensate for type changes to the compiler.

0
source

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


All Articles