471,317 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Get default(T) given Type representing T

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
Aug 29 '08 #1
15 1277
On Fri, 29 Aug 2008 11:28:39 -0700, je**********@gmail.com
<je**********@gmail.comwrote:
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)?
Could you be more specific? Generally, you can just write "default(T)"
where T is the type parameter for your generic type and it will work.

Taking as an assumption that that's _not_ working for you, it would be
helpful if you could explain why. What are you doing that causes that
approach to fail? Some sample code would help.

Pete
Aug 29 '08 #2

<je**********@gmail.comwrote in message
news:f6**********************************@8g2000hs e.googlegroups.com...
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?
--
Anthony Jones - MVP ASP/ASP.NET
Aug 29 '08 #3
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;
}
Aug 29 '08 #4
je**********@gmail.com wrote:
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
Aug 29 '08 #5
On Fri, 29 Aug 2008 11:51:01 -0700, je**********@gmail.com
<je**********@gmail.comwrote:
I am doing this at runtime. If it was at compile time, I would just
use T directly.
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 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 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
Aug 29 '08 #6
"Arne Vajhj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
je**********@gmail.com wrote:
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() !
Why? The default for all reference types is null.

--
Anthony Jones - MVP ASP/ASP.NET
Aug 29 '08 #7
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?
Aug 29 '08 #8
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 29 Aug 2008 11:51:01 -0700, je**********@gmail.com
<je**********@gmail.comwrote:
I am doing this at runtime. If it was at compile time, I would just
use T directly.

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 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 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.
--
Anthony Jones - MVP ASP/ASP.NET
Aug 29 '08 #9
On Aug 29, 1:13*pm, "Anthony Jones" <A...@yadayadayada.comwrote:
"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message

news:op***************@petes-computer.local...


On Fri, 29 Aug 2008 11:51:01 -0700, jehugalea...@gmail.com
<jehugalea...@gmail.comwrote:
I am doing this at runtime. If it was at compile time, I would just
use T directly.
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 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 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 wouldbox
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.

--
Anthony Jones - MVP ASP/ASP.NET- Hide quoted text -

- Show quoted text -
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.
Aug 29 '08 #10
On Fri, 29 Aug 2008 12:11:56 -0700, je**********@gmail.com
<je**********@gmail.comwrote:
[...]
>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?

Is there a way to avoid the boxing?
Without knowing the actual type? No, not really. To unbox a value type,
you need a place to put it. The only way to get a place to put it is to
know the type at compile-time (either through the use of a generic, or
simply being explicitly typed).

Pete
Aug 29 '08 #11
Anthony Jones wrote:
"Arne Vajhj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
>je**********@gmail.com wrote:
>>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() !

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
Aug 29 '08 #12
On Aug 29, 1:13*pm, "Anthony Jones" <A...@yadayadayada.comwrote:
"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message

news:op***************@petes-computer.local...


On Fri, 29 Aug 2008 11:51:01 -0700, jehugalea...@gmail.com
<jehugalea...@gmail.comwrote:
I am doing this at runtime. If it was at compile time, I would just
use T directly.
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 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 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 wouldbox
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.

--
Anthony Jones - MVP ASP/ASP.NET- Hide quoted text -

- Show quoted text -
Good point about returning an object. Boxing is inevitable.
Aug 29 '08 #13
On Aug 29, 11:04*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
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 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.

Aug 31 '08 #14
On Sun, 31 Aug 2008 01:29:50 -0700, Pavel Minaev <in****@gmail.comwrote:
It seems pretty clear to me. He is trying to write something like
this:

object GetDefaultValue(Type type);
Sure. You have the benefit of having the whole thread to look at now. :p

(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).
in which case his code would indeed work.
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.

Pete
Aug 31 '08 #15
Peter Duniho wrote:
(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 :)
in which case his code would indeed work.

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?
Aug 31 '08 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by John L. Clark | last post: by
10 posts views Thread by cppaddict | last post: by
29 posts views Thread by shmartonak | last post: by
29 posts views Thread by John Wood | last post: by
3 posts views Thread by JustSomeGuy | last post: by
74 posts views Thread by Zytan | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.