473,473 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I get the type of the elements underlying an enumerable object

This is what I mean: I have the an enumerable object (implements
IEnumerable =GetEnumerator() method) and I would like to find out
what types underly this enumerable object. For example, if I have the
following:

int[] numbers = new int[] {1,2,3}

I can do this:

foreach(int number in numbers)
{
...
}

I want to find a way to get the type of the elements of the "numbers"
object, which is "int" in this case, because I can enumerate over
"int" objects. I need this to also work if the elements are null.
The construct where I would find out the type of the underlying
elements would look like this:

foreach (object o in enumerableObject)
{
if (o == null)
{
//TODO: find out the actual type of "o" (remember "o" is
null), so I have to somehow use the enumerableObject
}
}

Mar 29 '07 #1
1 2031
Hermann,

Instead of using IEnumerable, why not use the generic version,
IEnumerable<T>. In this case, you would use Nullable<intfor T (or the
alias, int?).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Hermann Klinke" <h.******@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
This is what I mean: I have the an enumerable object (implements
IEnumerable =GetEnumerator() method) and I would like to find out
what types underly this enumerable object. For example, if I have the
following:

int[] numbers = new int[] {1,2,3}

I can do this:

foreach(int number in numbers)
{
...
}

I want to find a way to get the type of the elements of the "numbers"
object, which is "int" in this case, because I can enumerate over
"int" objects. I need this to also work if the elements are null.
The construct where I would find out the type of the underlying
elements would look like this:

foreach (object o in enumerableObject)
{
if (o == null)
{
//TODO: find out the actual type of "o" (remember "o" is
null), so I have to somehow use the enumerableObject
}
}

Mar 30 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christopher Benson-Manica | last post by:
I'd like to add a method foo() to all objects: Object.prototype.foo=function() { // whatever } The caveat, though, is that I do not want foo to be enumerable by a for-in loop. Can I do...
13
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} };...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
4
by: Harris | last post by:
Does anyone know how to get the underlying type of an Array type? Here's a bit of code demonstrating what I'm looking for... public Type getUnderlyingType( Type arrayType ) { if(...
10
by: J055 | last post by:
Hi I have an OrderedDictionary object where the key is an enum. Is there an easy way to cast it to an integer? Examples/document appreciated. Thanks Andrew
1
by: =?Utf-8?B?RnJhbmNvaXNWaWxqb2Vu?= | last post by:
Hi there, Does anybody know how to return the results of an IEnumerable<typeas an array of the same type i.e type in a web service call without first having to collect all elements in the...
1
by: Chris | last post by:
Hi, I want to limit the amount of data shown in a page coming from a database. Everything works except that I get the error: "Unable to cast object of type...
1
by: dhtmlkitchen | last post by:
I think this is not possible... var fnProto = Function().prototype; var badAss = { constructor : "Chuck Norris" }; result = ; function isConstructorEnumerable( ocean, pacific ) {
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.