IsArray?

N

Norbert Pürringer

Hello again,

I've got a type variable, e.g.

Type type;

Now I want to know whether the type represents an Array type
(System.Array).

type.IsArray results false because System.Array is an object, not an
array.

How can I find out, whether the type variable represents System.Array?

The way type.Name == "System.Array" is not really good, isn't it?

Norbert
 
J

Jon Skeet [C# MVP]

I've got a type variable, e.g.

Type type;

Now I want to know whether the type represents an Array type
(System.Array).

type.IsArray results false because System.Array is an object, not an
array.

What exactly do you mean by that? type.IsArray should be fine.

Could you post a short but complete program demonstrating the problem?

Jon
 
J

Jon Skeet [C# MVP]

Jon Skeet said:
What exactly do you mean by that? type.IsArray should be fine.

typeof(anything[]).IsArray is true, but typeof(Array).IsArray is false.

As the docs (for IsArray) state: "The Array class returns false
because it is not an array."

Not exactly clear, but my guess is it's because Array is abstract -
you can't have an instance of just Array, and various array-like
things don't make sense for it (such as element type).

Jon
 
C

Chris Shepherd

Norbert said:
Hello again,

I've got a type variable, e.g.

Type type;

Now I want to know whether the type represents an Array type
(System.Array).

type.IsArray results false because System.Array is an object, not an
array.

How can I find out, whether the type variable represents System.Array?

Could to not see if the type "is IEnumerable"? I could be way off here but I
thought most if not all array types were implicitly IEnumerable.

Chris.
 
M

Marc Gravell

Well, Array is abstract, so you will never actually have an Array
instance - just things that subclass it. But if you really must cater
for both T[] and Array, then perhaps just:

bool isArray = type.IsArray || type == typeof(Array);

Marc
 
B

Ben Voigt [C++ MVP]

Chris said:
Could to not see if the type "is IEnumerable"? I could be way off
here but I thought most if not all array types were implicitly
IEnumerable.

Sure... but so are many things that aren't arrays.
 
C

Chris Shepherd

Ben said:
Sure... but so are many things that aren't arrays.

I should have been clearer -- I was mentioning arrays in the sense of the
logical construct vs the literal class Array.

The OP should probably clarify the exact use scenario he's looking for.

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top