Michael Bray <mbray@makeDIntoDot_ctiusaDcomwrote:
Lets say I want to get the default value for a system type... how would I
implement the following function:
>
public object DefaultValueOfType(Type t)
{
return ????;
}
>
I want to be able to call it like this:
>
DefaultValueOfType(typeof(string));
DefaultValueOfType(typeof(int));
DefaultValueOfType(typeof(double));
For reference types, the default value is always null. For value types,
the default value can be obtained by calling the parameterless
constructor which is guaranteed to exist:
static object DefaultValueOfType (Type t)
{
return t.IsValueType ? Activator.CreateInstance(t)
: null;
}
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too