7

I was about to use Enum.GetValues() as normal, and started wondering why it returns a System.Array instead of object[]. Is it something to do with the ComVisible attribute?

This led to the question, what is the difference between Array and object[], if any?

I know Array can be used for static methods whilst T[] cannot but is there a difference for instances? They seem to have the same instance methods/properties.

If there is no difference, why does Enum.GetValues() return Array instead of object[]?

2
  • 2
    That is just the declaration, valid because Array is the base type of any array. You actually get an T[] back, where T is the enum type. .NET 1.x did not yet have generics. Commented Apr 4, 2018 at 18:17
  • 1
    ´Enum.GetValues´ is from before the age of generics. It's easy to write a generic wrapper for it that returns the correct type. Commented Apr 4, 2018 at 18:26

1 Answer 1

11

I was about to use Enum.GetValues() as normal, and started wondering why it returns a System.Array instead of object[].

Because object[] would box all the values. What we want is an array of the values themselves, not their boxed versions.

The only way to represent that generically in pre-generics C# is Array. Remember, that API was in the first version of the CLR, which lacked generics.

There are a number of better possible designs in a world with generics.

Frankly, there are better designs in a world without generics too. Two that immediately come to mind are: (1) This could have been an instance method on Type, (2) the C# compiler could have generated a public static MyEnum[] GetValues() method on every enum type.

Is it something to do with the ComVisible attribute?

Not to my knowledge.

What is the difference between Array and object[], if any?

I don't know how to answer "what's the difference" questions. If I asked you "what's the difference between a giraffe and a mammal?" how would you answer it? It's a bizarre question.

Array is the base class of object[] is the best I can tell you.

I know Array can be used for static methods whilst T[] cannot but is there a difference for instances?

I don't understand this question either. Can you give an example of what you mean by "can be used for static methods"?

Perhaps what you mean is the oddity that usually you can access the static members of a type from a derived type, but you cannot with array types? That is, you can say:

class B { public static void M() {} } 
class D : B {}
...
B.M();
D.M(); // Same thing

And this works:

Array.Sort(whatever);

But this does not work:

int[].Sort(whatever);

That's an oddity of how C# processes member access operators, but it's an oddity that hurts nothing because if you're typing int[].Sort() odds are pretty good you're doing something wrong.

Either way, I don't see what it has to do with your question. Can you clarify the question?

If there is no difference, why does Enum.GetValues() return Array instead of object[]?

This question is incoherent. If you told me that "The White House" and "1600 Pennsylvania Avenue" were the same address, and I then said "well, if there's no difference, then why does President Trump live at The White House instead of 1600 Pennsylvania Avenue?" how would you answer my crazy question? If there's no difference then there's no difference, so why does it matter?

In this case, the question is based on a false premise, as object[] and Array are different. But the question as it stands cannot be answered.

Sign up to request clarification or add additional context in comments.

7 Comments

Array is the base class of object[] is the best I can tell you. Thanks for this line. I always thought [] was core to the framework and Array used object[] underneath.
@TyCobb: Arrays are surprisingly complicated in the CLR; there are features in the CLR array type system that C# does not surface. For example, the CLR lets you define an array type like int[10..20], that is, a one-dimensional array of ints with lower bound index 10 and upper bound index 20, in the type system itself, not just at runtime. C# does not support such types, obviously.
@TyCobb: But yes, Array is simply a base type of all array types, just as Delegate is a base type of all delegate types, Enum is a base type of all enum types, and ValueType is a base type of all value types. Oddly enough, Nullable, the non-generic version, is not the base type of all nullable types.
There's a lot of extra information here but the basic answer seems to be that Array is the base class for object[] (and all T[]). I was thinking that object[] was the base class for all T[]s but now I see that that's not right. And the return type of Enum.GetValues() isn't EnumType[] because generics didn't exist back then. Also, not sure why it's so difficult for you to answer the giraffe/mammal question, a giraffe is a specific species while a mammal is any species in the mammal group.
@GustyAbra: No, absolutely a giraffe is not a species. Giraffa camelopardalis is a species. A giraffe is a giraffe! What's the difference between the set of all giraffes and the set of all mammals? What's the difference between the abstract concept of giraffeness and the abstract concept of mammalness? Do those two questions have the same, or different answers? Now perhaps you begin to see why the "what's the difference" question is subtle.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.