473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 1375
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 Vajhøj" <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 Vajhøj" <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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a...
10
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
29
by: John Wood | last post by:
Even though CSC does its best to detect use of unassigned variables, it often misses it... for example if you just declare a double in a class without assigning a default value, it has a default...
3
by: JustSomeGuy | last post by:
Hi I have a function that takes a refrence to a std::list<myType> & for ex: myMethod(int a=0, int b=0, std::list<myType> & lst=NULL) Now most of the time the lst is not passed and so I would...
12
by: Edward Diener | last post by:
Given value class X { public: // Not allowed: X():i(100000),s(10000) { } // Allowed void InitializeDefaults() { i = 100000; s = 10000; } private: int i;
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.