TheSwamp

Code Red => .NET => Topic started by: Keith Brown on February 16, 2013, 10:17:02 AM

Title: Use 'var' keyword when initializer explicitly declares type
Post by: Keith Brown on February 16, 2013, 10:17:02 AM
I have downloaded a trial of Resharper and just giving it a run through when and it has suggested that I use the var keyword when the initializer explicitly declares the type.  Here is an example.

Code - C#: [Select]
  1. PromptKeywordOptions getDwgPropertiesOptions = new PromptKeywordOptions(commandText, commandKeywords);

and it suggests that I change it to

Code - C#: [Select]
  1. var getDwgPropertiesOptions = new PromptKeywordOptions(commandText, commandKeywords);

my question is why?  Is it just a style thing or does the answer lie more deeply in the underpinnings of C#?
Title: Re: Use 'var' keyword when initializer explicitly declares type
Post by: Keith™ on February 16, 2013, 10:52:12 AM
var is used to designate an implicitly defined variable, not an explicitly defined one.

According to Microsoft, the reason this is the case is because the type for implicitly defined variables is known only by the compiler and there is also an efficiency improvement with explicitly defined variables (according to microsoft).

There are competing thoughts on using var instead of explicit typing. I generally use explicit typing whenever possible.
Title: Re: Use 'var' keyword when initializer explicitly declares type
Post by: Keith Brown on February 16, 2013, 10:56:30 AM
var is used to designate an implicitly defined variable, not an explicitly defined one.

Exactly.  I was explicitly defining the variable and it was suggesting that I implicitly define it.    If there are performance improvements to be had I was just wondering why it would suggest the change.
Title: Re: Use 'var' keyword when initializer explicitly declares type
Post by: Keith™ on February 16, 2013, 11:30:17 AM
Well, I am not sure there are performance improvements, just that Microsoft claims there are ;-)

Some folks have disputed the performance improvement claim by pointing out that even variables defined by var are still strongly typed, just not in the pre-compile sense.

A quick check of the Resharper settings indicates that you can change the notification for this.

Like I said, there is quite a variety of opinion on var, both for and against its use, much like any other tool that is in the programmers toolbox. You should consider the pros and cons for yourself.

For me, the pro is that as long as I am using self-documenting code, var can improve readability, and the biggest con is that the IDE does not always catch subtle differences in variable types and can lead to data loss or unexpected results, for example when implicit type conversion happens between ints, doubles, longs etc.

Also, keep in mind that you must initialize with a non-null value if you use var
Title: Re: Use 'var' keyword when initializer explicitly declares type
Post by: n.yuan on February 16, 2013, 12:23:25 PM
Since C# 3.0, there is case you have to use 'var' - Anonymous Class. For example, you have a class:

public class MyClass
{
    public int Filed1 { set; get; }
    public int Filed2 { set; get; }
    public string Filed3 { set; get; }
    public string Filed4 { set; get; }
}

And you get a list of objects of this class from somewhere (such as back end services from the cloud):

IEnumarable<MyClass> backendData=GetDataFromService(...);

You want to bind the data to a UI, which only need 2 fields from the class.

One way is to define another class explicitly and fill data to it. But you can more easily do this:

Code: [Select]
var bindingData=from c in backendData
where ....select new { Number=c.Field1, Name=c.Field3 };

In this case, 'select new {...} defines an anonymous class, which is strongly typed, but without a class name (hence Anonymous). Here you can only use 'var' to the strong typed IEnumerable collection.

In the popular frontend/backend services world it is common the front end has no control on what back end services can provide, but using Anonymous class can make it easy and more effecient for consuming data from the services and only getting what it needs,