473,322 Members | 1,314 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,322 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 2035
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
10
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
12
by: junky_fellow | last post by:
How can I declare a function that returns a pointer to one dimensional array ?
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
21
by: subramanian100in | last post by:
Suppose we have char array; C allows taking the address of array (ie &array is valid) though the element array cannot be accessed. Is this allowed for use in binary search method (as given...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.