开发者

Which one is better to use and why in c#

开发者 https://www.devze.com 2023-01-06 14:32 出处:网络
Which one i开发者_StackOverflows better to use? int xyz = 0; OR int xyz= default(int);int xyz = 0; Why make people think more than necessary?default is useful with generic code, but here it does

Which one i开发者_StackOverflows better to use?

int xyz = 0;

OR

int xyz= default(int);


int xyz = 0;

Why make people think more than necessary? default is useful with generic code, but here it doesn't add anything. You should also think if you're initializing it in the right place, with a meaningful value. Sometimes you see, with stack variables, code like:

int xyz = 0;

if(someCondition)
{
  // ...
  xyz = 1;
  // ...
}
else
{
  // ...
  xyz = 2;
  // ...
}

In such cases, you should delay initialization until you have the real value. Do:

int xyz;

if(someCondition)
{
  // ...
  xyz = 1;
  // ...
}
else
{
  // ...
  xyz = 2;
  // ...
}

The compiler ensures you don't use an uninitialized stack variable. In some cases, you have to use meaningless values because the compiler can't know code will never execute (due to an exception, call to Exit, etc.). This is the exception (no pun intended) to the rule.


It depends what you want to achieve.

I would prefer

int xyz = 0;

as I believe it is more readable and not confusing.

default keyword is mostly suitable for Generics.


The purpose of the default operator is to provide you with the default value for a type, but it was added primarily to allow generics to have a valid value for values declared to be of its generic type arguments.

I don't have hard evidence but I suspect the compiler will emit the same code for both in your specific case.

However, here there is a legitimate use of default:

public T Aggregate<T>(IEnumerable<T> collection, Func<T, T, T> aggregation)
{
    T result = default(T);

    foreach (T element in collection)
        result = aggregation(result, element);

    return result;
}

Without default, the above code would need some hacks in order to compile and function properly.

So use the first, set it to 0.


no performance difference between your codes. to see clearly use int xyz = 0;


Given that the emitted CIL is identical (you get

  IL_0001:  ldc.i4.0
  IL_0002:  stloc.0

in both cases), the rule is to choose the one you feel better communicates the intent of the code. Normally, questions of feeling are subjective and hard-to-decide; in this case, however, were I the code reviewer, I would have to be presented with an extremely compelling reason to accept what looks at first sight to be an entirely superfluous use of default().


int xyz = default(int); I like this way when working with Generics bcoz it give you flexibility to get default of whatever type you are working with.

int xyz=0; On the other hand this is easy and fast and obviously won't work in generic cases.

Both have their pros and cons..

Regards,


int xyz = 0 is moreclear, defaut is generally used with generics


the best is

int xyz;

because you can't access to uninitialized variable.

0

精彩评论

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

关注公众号