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.
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.
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();
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.
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.
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.)
Maybe Type.GetElementType() is what you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With