Tutorial :Which one is better to use and why in c#



Question:

Which one is better to use?

int xyz = 0;  

OR

int xyz= default(int);


Solution:1

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.


Solution:2

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.


Solution:3

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.


Solution:4

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


Solution:5

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().


Solution:6

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,


Solution:7

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


Solution:8

the best is

int xyz;  

because you can't access to uninitialized variable.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »