471,315 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Array element type

What is the way to the type of an array's elements?

Array a = new long[] { 1, 2, 3 };
Type t = a.GetType();
Console.WriteLine( t.FullName );

This will write "System.Int64[]", but is not what I'm looking for.
Rather I want the type of the array's elements, so "System.Int64". I
seem to remember something along the lines of "a.GetElementType()" but
I can 't find anyting of the sort anymore.

Cheers

Jan 26 '06 #1
5 1917
Nevermind, found it...

Array a = new long[] { 1, 2, 3 };
Type t = a.GetType().GetElementType();
Console.WriteLine( t.FullName );

Jan 26 '06 #2
<jt*****@gmail.com> wrote:
What is the way to the type of an array's elements?
Type.GetElementType()
Array a = new long[] { 1, 2, 3 };
Type t = a.GetType();
Console.WriteLine( t.FullName );

This will write "System.Int64[]", but is not what I'm looking for.
Rather I want the type of the array's elements, so "System.Int64". I
seem to remember something along the lines of "a.GetElementType()" but
I can 't find anyting of the sort anymore.


Right method, wrong type :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 26 '06 #3
Hi,

What about

Type t = a[0].GetType();

The method I believe you said you forgot I guess is

Type t = a.GetType().GetElementType();

GetElementType is method of the Type class not the Array class.
--
HTH
Stoitcho Goutsev (100)

<jt*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
What is the way to the type of an array's elements?

Array a = new long[] { 1, 2, 3 };
Type t = a.GetType();
Console.WriteLine( t.FullName );

This will write "System.Int64[]", but is not what I'm looking for.
Rather I want the type of the array's elements, so "System.Int64". I
seem to remember something along the lines of "a.GetElementType()" but
I can 't find anyting of the sort anymore.

Cheers

Jan 26 '06 #4
Stoitcho Goutsev (100) <10*@100.com> wrote:
What about

Type t = a[0].GetType();
There are three problems with that:

1) The array might be empty
2) For reference types, the first element might be null
3) For reference types, the type of the actual object referred to could
be derived from the type of the array element (eg object[] with the
first element being a string)
The method I believe you said you forgot I guess is

Type t = a.GetType().GetElementType();

GetElementType is method of the Type class not the Array class.


Yup.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 26 '06 #5
>> What about

Type t = a[0].GetType();


There are three problems with that:

1) The array might be empty
2) For reference types, the first element might be null
3) For reference types, the type of the actual object referred to could
be derived from the type of the array element (eg object[] with the
first element being a string)


Yes, I know that. I was refering to the concrete example. That was most like
a joke :) This was the reason I gave the real solution too.
Thanks for the clarification.
--

Stoitcho Goutsev (100)
Jan 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Adam Warner | last post: by
24 posts views Thread by Michael | last post: by
21 posts views Thread by subramanian100in | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
reply views Thread by rosydwin | 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.