PC Review


Reply
Thread Tools Rate Thread

Default value of any type.

 
 
GeezerButler
Guest
Posts: n/a
 
      14th Nov 2007
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 !

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      14th Nov 2007
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<T> you will get null, not an
boxed empty value - which is probably what you want anyway.

Marc


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      14th Nov 2007
(copy paste glitch - should have read

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


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th Nov 2007
On Nov 14, 11:00 am, GeezerButler <kurtr...@gmail.com> 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 !


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

 
Reply With Quote
 
GeezerButler
Guest
Posts: n/a
 
      14th Nov 2007
Thanks a lot guys!

 
Reply With Quote
 
Alun Harford
Guest
Posts: n/a
 
      14th Nov 2007
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").MakeGenericMethod(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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th Nov 2007
Alun Harford <(E-Mail Removed)> wrote:

<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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      15th Nov 2007
> 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



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Nov 2007
Ben Voigt [C++ MVP] <(E-Mail Removed)> 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.


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,object> cache =
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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Alun Harford
Guest
Posts: n/a
 
      15th Nov 2007
Jon Skeet [C# MVP] wrote:
> Alun Harford <(E-Mail Removed)> wrote:
>
> <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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find default value for value type Andrus Microsoft C# .NET 2 23rd Oct 2009 03:49 AM
Default value of any type GeezerButler Microsoft Dot NET Framework 3 14th Nov 2007 02:25 PM
Default reference type Russ.Dilley@gmail.com Microsoft Excel Misc 3 11th Oct 2006 06:35 PM
How to set default save as type to mht? Shetty Windows XP Internet Explorer 1 4th Aug 2004 02:09 PM
default value of type Franz Microsoft C# .NET 3 1st Sep 2003 02:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:46 AM.