Get default(T) given Type representing T

  • Thread starter Thread starter jehugaleahsa
  • Start date Start date
J

jehugaleahsa

Hello:

I would like to get default(T). However, I don't know what T is until
runtime; I just have a Type. Is there an equivilent means of getting
the results of default(T)?

Thanks,
Travis
 
Hello:

I would like to get default(T). However, I don't know what T is until
runtime; I just have a Type. Is there an equivilent means of getting
the results of default(T)?

Its a little ambiguous. If T is a type parameter then default(T) _is_ the
answer.

If T is simply a place marker for your unknown T then how is it unknown?
Do you want to create a default value for the type of an object referenced
by an object type?

A bit more code would help direct us?
 
I am doing this at runtime. If it was at compile time, I would just
use T directly.

I have an instance of Type. I would like to emulate the functionality
that default(T) provides. Would this work:

if (type.IsValueType)
{
return Activator.CreateInstance(type);
}
else
{
return null;
}
 
I would like to get default(T). However, I don't know what T is until
runtime; I just have a Type. Is there an equivilent means of getting
the results of default(T)?

default(T) is intended for just that !

http://msdn.microsoft.com/en-us/library/25tdedf5.aspx

<quote>
Generic code: Specifies the default value of the type parameter. This
will be null for reference types and zero for value types.
</quote>

If it is a reference type you may want to put a restriction on
T tht is must have a parameterless constructor and use new T() !

Arne
 
Arne Vajhøj said:
default(T) is intended for just that !

http://msdn.microsoft.com/en-us/library/25tdedf5.aspx

<quote>
Generic code: Specifies the default value of the type parameter. This
will be null for reference types and zero for value types.
</quote>

If it is a reference type you may want to put a restriction on
T tht is must have a parameterless constructor and use new T() !

Why? The default for all reference types is null.
 
I have no idea.  We still don't really know what you're doing.  Your  
question was presented as if it was in the context of a generic type or  
method, but now I'm not so sure.

The code you posted certainly _could_ do what you want.  But it would box  
the value type as returned.  That's not exactly the same as what  
"default(T)" would return.  But if the code accomplishes your goal, that  
seems to me to be the same as "would work".  Right?

Pete

Is there a way to avoid the boxing?
 
Peter Duniho said:
For what it's worth, it's hard for me to understand your distinction
between "doing this at runtime" and "at compile time". In particular,
while the code is written before "compile time", none of the code executes
until "runtime".

From your elaboration, I gather that what you really mean is that you
don't actually have even a type parameter T to work with (again, some
actual sample code would have been helpful in expressing the question).
With that in mind...


I have no idea. We still don't really know what you're doing. Your
question was presented as if it was in the context of a generic type or
method, but now I'm not so sure.

The code you posted certainly _could_ do what you want. But it would box
the value type as returned. That's not exactly the same as what
"default(T)" would return. But if the code accomplishes your goal, that
seems to me to be the same as "would work". Right?

If the function is a generic type then according to the posted code it has
to be returning object anyway.

I suspect the OP hasn't really looked into Generics.
 
If the function is a generic type then according to the posted code it has
to be returning object anyway.

I suspect the OP hasn't really looked into Generics.

Unfortunately, I can't use generics at this low level. This code is
using reflection to move data between data object properties and a
DataRow columns. I don't know the type I am converting to, since all I
am given is a PropertyInfo. All I have to work with an
PropertyInfo.GetValue and PropertyInfo.PropertyType.
 
Anthony said:
Why? The default for all reference types is null.

Exactly.

(and also what it is said in the text I quoted)

Which is why I think the original poster may be interested
in new T() instead of default(T) for reference types.

null is usually not so useful.

Arne
 
If the function is a generic type then according to the posted code it has
to be returning object anyway.

I suspect the OP hasn't really looked into Generics.

Good point about returning an object. Boxing is inevitable.
 
I have no idea.  We still don't really know what you're doing.  Your  
question was presented as if it was in the context of a generic type or  
method, but now I'm not so sure.

It seems pretty clear to me. He is trying to write something like
this:

object GetDefaultValue(Type type);

in which case his code would indeed work.
 
Peter said:
(Not one of the three of us who replied to the original post figured out
the desired behavior from that original post...I think that's sufficient
evidence that the question was ambiguous at best; we can't have _all_
missed something obvious, even if I myself have been known to do so from
time to time).

Well, I've figured it from the very first post, but it may help that
I'm _not_ a native English speaker :)
Well, maybe. It certainly doesn't do the exact same thing that the
"default" operator would. In particular, he would get a boxed value type
back.

For the method with the sig given, it's irrelevant, since it'd have to
return a boxed value anyway. It would be possible to make a generator
returning unboxed values using a generic method and a bit of
reflection, but since it would still be boxed to object when used, why
bother?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top