开发者

Use of var and default for declaration in C#

Recently I saw a person heavily using var and default keywords for declaration of variables (and for every declaration), something like this:

var employee = default(Employee);   //Employee is a class
var errorInfo = default(ErrorInfo); //ErrorInfo is str开发者_如何转开发uct; Blank is default value
var salary = default(Double);
var isManager = default(Boolean?); 

instead of using:

Employee employee = null;           //Employee is a class
ErrorInfo errorInfo = Blank;        //ErrorInfo is struct; Blank is default value
Double salary = 0.0;
Boolean? isManager = null;

or, instead of using even:

Employee employee;                  //Employee is a class
ErrorInfo errorInfo;                //ErrorInfo is struct; Blank is default value
Double salary;
Boolean? isManager;

Now using var and default for declaration for every variable is something i am not accustomed to.

Want to know:

- If this is a recommended practice?

- Your views and preference?

PS:

- Have gone through Use of var keyword in C#, Use of "var" type in variable declaration and https://stackoverflow.com/questions/633474/c-do-you-use-var, however, think that this question although related is slightly different as it is solely around declaration/initialization and not around assignment.

- I understand the difference between snipped 2 and snippet 3. However, question is more around snippet 1.

- If you strongly feel that this question belongs to programmers stackexchange feel free to move.


I'm not going to say anything about "var" there have been comments and discussions about this in the past (sufficiently so ;-)

Concerning "default()" I would not use this to initialize a known type, but rather only in generics. There it helps to transparently handle value types or reference types by allowing you to provide a default (return) value or can be used in comparisons.


Well, the default keyword isn't the most used keyword I think, and in my opinion it serves its purpose best in terms of Generics, like so:

public class Foo<T>{

    private T _instance;
    public Foo<T>(){
        _instance = default(T);
    }
}

in order to get a new default instance of T. There are really no reasons to use it like scenario 1 in your post.

Using var however is a different question, and many view this as a matter of readability. I default to var when I write code simply because I find it easier to read:

var me = new Person();

It seems a bit redundant in terms of readability to do

Person me = new Person();

Another case I recommend var is if something changes. Consider the following example:

public decimal Calculate(decimal one, decimal two){
    return one + two;
}

And somewhere else in your code:

decimal d = Calculate(1M, 2M);

If you for some reason change the return type of Calculate to, say, double you need to change all the places where you strongly defined the variable.

If you instead do

var d = Calculate(1M, 2M)

you don't have to worry about this. The Double/Decimal example is a bit simple, but in terms of refactoring and interfacing out classes, I've found this very useful.


I think this is bad practice which will prevent the compiler (and 3rd party tools) from catching bugs related to failure to initialize a variable. Generally I try to keep declaration and assignment as close to each other as possible. Assigning values that aren't intended to be used to variables can potentially introduce subtle bugs that are difficult to catch. Normally I'd either:

SomeType variable1; //want to store something that will be out of scope later
using(blah)
{
    //...
    variable1=blah;
}
//use variable1 here

or assign required value immediately:

SomeType variable2 = new SomeType();
//use variable2 immediately

or (for me, more frequently nowdays)

var variable2 = new SomeType();

assigning null/placeholder values is mainly pointless.


I use var for assignment. However I always declare instances using the class. I generally also instantiate them at the time to avoid unexpected NullReferenceExceptions


The code is ok.

Just make sure that you don't copy this technique to initialize enums where 0 is not default value or flagged enumerations.

[Flags]
public enum MyFlags
{
    Test = 1,
    Test2 = 2
}

MyFlags flags = default(MyFlags);
Console.WriteLine(flags); // oops
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜