Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the Array Item Type from Array Type in .net

Say I have an System.String[] type object. I can query the type object to determine if it is an array

Type t1 = typeof(System.String[]); bool isAnArray = t1.IsArray; // should be true 

However how do I get a type object of the array item from t1

Type t2 = ....; // should be typeof(System.String) 
like image 842
Preet Sangha Avatar asked Nov 09 '10 01:11

Preet Sangha


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 do you check if an object is an array or not in C#?

To do this task, we use the IsArray property of the Type class. This property is used to determine whether the specified type is an array or not. IsArray property will return true if the specified type is an array. Otherwise, it will return false.

Is array a reference type in c#?

Array, which itself is derived from System. Object. This means that all arrays are always reference types which are allocated on the managed heap, and your app's variable contains a reference to the array and not the array itself.

Can you store different types in an array in C#?

Answer: In C# programming, Array is homogeneous in nature, So, It cannot hold multiple data types e.g. int, String and bool etc. However, by declaring an array as object [] can be used to store different data type.


2 Answers

You can use the instance method Type.GetElementType for this purpose.

Type t2 = t1.GetElementType(); 

[Returns] the type of the object encompassed or referred to by the current array, pointer, or reference type, or null if the current Type is not an array or a pointer, or is not passed by reference, or represents a generic type or a type parameter in the definition of a generic type or generic method.

like image 118
Ani Avatar answered Sep 27 '22 19:09

Ani


Thanks to @psaxton comment pointing out the difference between Array and other collections. As an extension method:

public static class TypeHelperExtensions {     /// <summary>     /// If the given <paramref name="type"/> is an array or some other collection     /// comprised of 0 or more instances of a "subtype", get that type     /// </summary>     /// <param name="type">the source type</param>     /// <returns></returns>     public static Type GetEnumeratedType(this Type type)     {         // provided by Array         var elType = type.GetElementType();         if (null != elType) return elType;          // otherwise provided by collection         var elTypes = type.GetGenericArguments();         if (elTypes.Length > 0) return elTypes[0];          // otherwise is not an 'enumerated' type         return null;     } } 

Usage:

typeof(Foo).GetEnumeratedType(); // null typeof(Foo[]).GetEnumeratedType(); // Foo typeof(List<Foo>).GetEnumeratedType(); // Foo typeof(ICollection<Foo>).GetEnumeratedType(); // Foo typeof(IEnumerable<Foo>).GetEnumeratedType(); // Foo  // some other oddities typeof(HashSet<Foo>).GetEnumeratedType(); // Foo typeof(Queue<Foo>).GetEnumeratedType(); // Foo typeof(Stack<Foo>).GetEnumeratedType(); // Foo typeof(Dictionary<int, Foo>).GetEnumeratedType(); // int typeof(Dictionary<Foo, int>).GetEnumeratedType(); // Foo, seems to work against key 
like image 22
drzaus Avatar answered Sep 27 '22 19:09

drzaus