469,636 Members | 1,544 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

Default value of any type.

For any given type i want to know its default value.

There is a neat keyword called default for doing this like
object x = default(DateTime);
but I have an instance of Type (called someType) and something like
this cant work
object x = default(someType);

Is there any nice way of doing this? I dont really want to write a
huge switch block enumerating all types in .NET !

Nov 14 '07 #1
14 31240
Generics might help, since default(T) will work fine...
If this isn't an option, you can make life a little simpler: only
value-types have a non-null result, and value-types have a default
ctor (kind-of) - so:

object obj = type.IsValueType ?
Activator.CreateInstance(type) :
obj = null;

Note that since it is boxed, for Nullable<Tyou will get null, not an
boxed empty value - which is probably what you want anyway.

Marc
Nov 14 '07 #2
(copy paste glitch - should have read:)

object obj = type.IsValueType ?
Activator.CreateInstance(type) : null;
Nov 14 '07 #3
On Nov 14, 11:00 am, GeezerButler <kurtr...@gmail.comwrote:
For any given type i want to know its default value.

There is a neat keyword called default for doing this like
object x = default(DateTime);
but I have an instance of Type (called someType) and something like
this cant work
object x = default(someType);

Is there any nice way of doing this? I dont really want to write a
huge switch block enumerating all types in .NET !
For value types, you can always use Activator.CreateInstance(someType)
as there's guaranteed to be a parameterless constructor.
For reference types, the default value is always null.

You can tell the difference using Type.IsValueType.

Jon

Nov 14 '07 #4
Thanks a lot guys!

Nov 14 '07 #5
GeezerButler wrote:
For any given type i want to know its default value.

There is a neat keyword called default for doing this like
object x = default(DateTime);
but I have an instance of Type (called someType) and something like
this cant work
object x = default(someType);

Is there any nice way of doing this? I dont really want to write a
huge switch block enumerating all types in .NET !
Basically, there isn't a very nice way. I have an extension method that
does something like (don't have the code on me):

public class Extension
{
public static object GetDefault(this Type type)
{
return
typeof(Extension).GetMethod("GetDefaultImp").MakeG enericMethod(type).Invoke(null,
new Type[0]);
}
public static T GetDefaultImp<T>()
{
return default(T);
}
}

(If you're not using C# 3, you can do that as plain old static utility
methods)

Note that this is fairly slow. If you need to do this lots, you're
better doing it 'properly' (checking to see if you've got a value or
reference type and returning Activator.CreateInstance(type) or null.

Alun Harford
Nov 14 '07 #6
Alun Harford <de*****@alunharford.co.ukwrote:

<snip>
Note that this is fairly slow. If you need to do this lots, you're
better doing it 'properly' (checking to see if you've got a value or
reference type and returning Activator.CreateInstance(type) or null.
Given that the "proper" way is only about 4 lines of code, what's the
advantage of using the MakeGenericMethod way?

--
Jon Skeet - <sk***@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
Nov 14 '07 #7
Note that this is fairly slow. If you need to do this lots, you're better
doing it 'properly' (checking to see if you've got a value or reference
type and returning Activator.CreateInstance(type) or null.
If you're going to do this lots, construct an appropriate delegate "delegate
object ObjectReturnerDelegate()" from the MethodInfo or ConstructorInfo and
cache this in a Dictionary<Type,ObjectReturnerDelegate>

A delegate call is much faster than reflection.
>
Alun Harford

Nov 15 '07 #8
Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:
Note that this is fairly slow. If you need to do this lots, you're better
doing it 'properly' (checking to see if you've got a value or reference
type and returning Activator.CreateInstance(type) or null.

If you're going to do this lots, construct an appropriate delegate "delegate
object ObjectReturnerDelegate()" from the MethodInfo or ConstructorInfo and
cache this in a Dictionary<Type,ObjectReturnerDelegate>

A delegate call is much faster than reflection.
Except that you've got to use reflection to *get* the method call in
the first place. Rather than caching a delegate, it makes more sense
(IMO) to cache the actual value - and only for value types, at that:

// Thread-safety not shown here
static readonly Dictionary<Type,objectcache =
new Dictionary<Type,object>();

static object GetDefaultValue(Type t)
{
if (!t.IsValueType)
{
return null;
}
object ret;
if (cache.TryGetValue(t, out ret))
{
return ret;
}
ret = Activator.CreateInstance(t);
cache[t] = ret;
return t;
}

--
Jon Skeet - <sk***@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
Nov 15 '07 #9
Jon Skeet [C# MVP] wrote:
Alun Harford <de*****@alunharford.co.ukwrote:

<snip>
>Note that this is fairly slow. If you need to do this lots, you're
better doing it 'properly' (checking to see if you've got a value or
reference type and returning Activator.CreateInstance(type) or null.

Given that the "proper" way is only about 4 lines of code, what's the
advantage of using the MakeGenericMethod way?
It's clearer, in my opinion.

Alun Harford
Nov 15 '07 #10
Ben Voigt [C++ MVP] wrote:
>Note that this is fairly slow. If you need to do this lots, you're better
doing it 'properly' (checking to see if you've got a value or reference
type and returning Activator.CreateInstance(type) or null.

If you're going to do this lots, construct an appropriate delegate "delegate
object ObjectReturnerDelegate()" from the MethodInfo or ConstructorInfo and
cache this in a Dictionary<Type,ObjectReturnerDelegate>

A delegate call is much faster than reflection.
If you're doing this lots, your design and/or choice of language is
probably very poor.

Alun Harford
Nov 15 '07 #11
Alun Harford <de*****@alunharford.co.ukwrote:
Given that the "proper" way is only about 4 lines of code, what's the
advantage of using the MakeGenericMethod way?

It's clearer, in my opinion.
Anyone reading that code has to understand generic methods and
accessing generic methods via reflection. With the simple
Activator.CreateInstance all you need to know is what CreateInstance
does (and that's reasonably common) and that all value types have a
parameterless constructor.

I certainly think CreateInstance is clearer and simpler. It's all a
matter of opinion though, yes.

--
Jon Skeet - <sk***@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
Nov 15 '07 #12
Jon Skeet [C# MVP] wrote:
Alun Harford <de*****@alunharford.co.ukwrote:
>>Given that the "proper" way is only about 4 lines of code, what's the
advantage of using the MakeGenericMethod way?
It's clearer, in my opinion.

Anyone reading that code has to understand generic methods and
accessing generic methods via reflection.
Except for by reflection, I can't think of many times you get an object
and don't know if it's a value type or a reference type.

The only other situation I can think of is interfacing with legacy
pre-generics code. Eugh! :-)

Alun Harford
Nov 15 '07 #13
Alun Harford <de*****@alunharford.co.ukwrote:
Anyone reading that code has to understand generic methods and
accessing generic methods via reflection.

Except for by reflection, I can't think of many times you get an object
and don't know if it's a value type or a reference type.
Absolutely - but when generics and reflection mix, life gets pretty
nasty in my experience. I'd rather avoid that wherever possible.

Another benefit is that the Activator.CreateInstance version works in
..NET 1.1 as well :)

--
Jon Skeet - <sk***@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
Nov 15 '07 #14

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:
Note that this is fairly slow. If you need to do this lots, you're
better
doing it 'properly' (checking to see if you've got a value or reference
type and returning Activator.CreateInstance(type) or null.

If you're going to do this lots, construct an appropriate delegate
"delegate
object ObjectReturnerDelegate()" from the MethodInfo or ConstructorInfo
and
cache this in a Dictionary<Type,ObjectReturnerDelegate>

A delegate call is much faster than reflection.

Except that you've got to use reflection to *get* the method call in
the first place. Rather than caching a delegate, it makes more sense
(IMO) to cache the actual value - and only for value types, at that:
Yes, I'm stupid :) Thanks for pointing out the obvious.

For cases other than the default constructor of a value type.... do what I
said.
Nov 15 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by David Lozzi | last post: by
21 posts views Thread by planetthoughtful | last post: by
7 posts views Thread by Justin | last post: by
4 posts views Thread by veerleverbr | last post: by
4 posts views Thread by Macneed | last post: by
9 posts views Thread by puzzlecracker | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.