Connect with Expertise | Find Experts, Get Answers, Share Insights

How to get a default instance of built-in type?

Michael Bray
 
Posts: n/a
#1: Dec 23 '06
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));

I have tried:

return t.TypeInitializer.Invoke(null);

but it says "Type initializer is not callable."

-mdb

=?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=
 
Posts: n/a
#2: Dec 23 '06

re: How to get a default instance of built-in type?


If you use generics you can use the default keyword...

Otherwise you can use Reflect to get the default c'tor, if it exists (i.e.
perform a check):
return t.GetConstructor(BindingFlags.Instance
| BindingFlags.Default
| BindingFlags.Public
, null
, Type.EmptyTypes
, null).Invoke(null);

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


"Michael Bray" wrote:
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));
>
I have tried:
>
return t.TypeInitializer.Invoke(null);
>
but it says "Type initializer is not callable."
>
-mdb
>
=?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=
 
Posts: n/a
#3: Dec 23 '06

re: How to get a default instance of built-in type?


....forgot an example for generics:
public static void GetDefaultValue<T( out T value )
{
value = default(T);
}
// ...
string text;
GetDefaultValue(out text);

Notice you don't have to deal with typeof at all... Keep in mind, that
won't create an instance for reference types--which will be null.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


"Michael Bray" wrote:
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));
>
I have tried:
>
return t.TypeInitializer.Invoke(null);
>
but it says "Type initializer is not callable."
>
-mdb
>
Jon Skeet [C# MVP]
 
Posts: n/a
#4: Dec 24 '06

re: How to get a default instance of built-in type?


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
=?UTF-8?B?QXJuZSBWYWpow7hq?=
 
Posts: n/a
#5: Dec 24 '06

re: How to get a default instance of built-in type?


Peter Ritchie [C# MVP] wrote:
Notice you don't have to deal with typeof at all... Keep in mind, that
won't create an instance for reference types--which will be null.
Maybe new was better than default.

public static T GetDefault<T>() where T : new()
{
return new T();
}

will work for reference types with a no arg constructor.

Arne
Closed Thread