363,924 Members | 2598 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

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

Michael Bray
P: n/a
Michael Bray
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
Dec 23 '06 #1
Share this Question
Share on Google+
4 Replies


=?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=
P: n/a
=?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=
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
>
Dec 23 '06 #2

=?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=
P: n/a
=?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=
....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
>
Dec 23 '06 #3

Jon Skeet [C# MVP]
P: n/a
Jon Skeet [C# MVP]
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
Dec 24 '06 #4

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

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp