I had some confusions about this thread.
What are differences between these terms? When should I use what?
int x = default(int);
int x = 0;
int x = new int();
I read here in this thread.
I had some confusions about this thread.
What are differences between these terms? When should I use what?
int x = default(int);
int x = 0;
int x = new int();
I read here in this thread.
They are all the same and as far as I can tell, they all result in the same IL:
ldc.i4.0
default
is a handy keyword for generics. You can initialize a variable to its default value even without knowing its type. It would then be a null for reference types, 0 for int and respective default values for other structs.
The constructor, new int()
is there because int
is just a (value) type like any other, so it has a constructor and you can call it.
Semantically new int()
and 0
are the same, the default
is a little bit different thing, but in case of integers it results in the same code.
Code example (I think the only thing that deserves an example is the default
keyword):
Suppose you have a simple implementation of DefaultIfEmpty
method (the code has been taken from edulinq and slightly modified - it's a great repository if you're interested in LINQ!). This method either yields the items of the source collection, or just one item, that is the default value of the specified generic type:
public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
IEnumerable<TSource> source)
{
bool foundAny = false;
foreach (TSource item in source)
{
yield return item;
foundAny = true;
}
if (!foundAny)
{
yield return // What to return here?
}
}
You want this to work on all types, but how do you get an instance of any type?
You don't know if it's and int
, so that you could write yield return 0
- that would not work when TSource
is string
. If you yield return null
, that won't work for int
...
Also some types might not have a parameterless constructor, some might not have an accessible constructor at all (a wierd case, but can happen, for example a collection containing some singleton instances) or maybe you have a collection of interface/abstract class type and you can't construct their instances (if you don't know another type implementing/inheriting them).
default
is there exactly for these cases. You don't know what type TSource
may be, but default (TSource)
will give you the default value anyway.
If you call
var collectionWithZero = new int[0].DefaultIfEmpty<int>();
you will get a collection with one item, a zero. The method got compiled with an int
as the TSource
, so it calls default (int)
and that results in 0. If you call the same method on an empty collection of strings, the method will be compiled for string as well and will use default (string)
, which results in null (because that is the default value for all reference types).
As for the other two (new int()
and 0
), I've already mentioned they mean the same, so just use the shorter one, zero. It's easier to read and to maintain, everyone knows what a zero is, so the constructor might only be confusing.
Always use
int x = 0;
or even just
int x;
It's more maintainable, better readable than the other versions. Also it's much likely that less questions of your peer programmers will occur.
They are all Same , just that framework offers different way to declare a variable . You can chose which ever you feel easy to use.