Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Array and object[] in C#?

Tags:

arrays

c#

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[]?

like image 488
Gusty Abra Avatar asked Apr 04 '18 18:04

Gusty Abra


1 Answers

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.

like image 187
Eric Lippert Avatar answered Sep 28 '22 07:09

Eric Lippert