Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetType() on Array item?

Tags:

arrays

c#

I have an initialised array that may contain no items.

Lets call it a,

Calling GetType() on a will obviously return a type of Array. Is it possible to get the type of the items the array contains?

Obviously a[0].GetType() would work, but then the array could be empty and cause a null reference exception.

like image 601
maxp Avatar asked Jan 18 '10 10:01

maxp


People also ask

How do you find the type of an object in an array?

In order to get the component type of an Array Object in Java, we use the getComponentType() method. The getComponentType() method returns the Class denoting the component type of an array. If the class is not an array class this method returns null.

How to get array type in C#?

The GetType() method of array class in C# gets the Type of the current instance (Inherited from Object). To get the type. Type tp = value. GetType();

How to use GetType in C#?

Is construct in Visual Basic or the is keyword in C#. The GetType method is inherited by all types that derive from Object. This means that, in addition to using your own language's comparison keyword, you can use the GetType method to determine the type of a particular object, as the following example shows.

What is array type in typescript?

In typescript, an array is a data type that can store multiple values of different data types sequentially. Similar to JavaScript, Typescript supports array declaration and there are multiple ways to do it. Declaring and Initializing Arrays: We can either use var or let for declaring an array.


2 Answers

Well, you can get the element type of the array:

Type type = array.GetType().GetElementType(); 

(That's not quite the same as getting the types of the items in the array - an object[] may be entirely populated with strings, for example.)

like image 119
Jon Skeet Avatar answered Sep 23 '22 05:09

Jon Skeet


Maybe Type.GetElementType() is what you need.

like image 22
Aggelos Biboudis Avatar answered Sep 25 '22 05:09

Aggelos Biboudis