Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is List.Sort() an instance method but Array.Sort() static?

Tags:

c#

.net

I'm trying to understand the design decision behind this part of the language. I admit i'm very new to it all but this is something which caught me out initially and I was wondering if I'm missing an obvious reason. Consider the following code:

List<int> MyList = new List<int>() { 5, 4, 3, 2, 1 }; int[] MyArray = {5,4,3,2,1};   //Sort the list MyList.Sort(); //This was an instance method   //Sort the Array Array.Sort(MyArray); //This was a static method 

Why are they not both implemented in the same way - intuitively to me it would make more sense if they were both instance methods?

like image 380
David Lee Avatar asked Jul 15 '11 22:07

David Lee


People also ask

Is sort a static method in Java?

The sort() method is a static method of Collections class.

Is indexOf static method?

indexOf() is a static method of the StringUtils used to find the index of the first occurrence of the search sequence in the given text.


2 Answers

The question is interesting because it reveals details of the .NET type system. Like value types, string and delegate types, array types get special treatment in .NET. The most notable oddish behavior is that you never explicitly declare an array type. The compiler takes care of it for you with ample helpings of the jitter. System.Array is an abstract type, you'll get dedicated array types in the process of writing code. Either by explicitly creating a type[] or by using generic classes that have an array in their base implementation.

In a largish program, having hundreds of array types is not unusual. Which is okay, but there's overhead involved for each type. It is storage required for just the type, not the objects of it. The biggest chunk of it is the so-called 'method table'. In a nutshell, it is a list of pointers to each instance method of the type. Both the class loader and the jitter work together to fill this table. This is commonly known as the 'v-table' but isn't quite a match, the table contains pointers to methods that are both non-virtual and virtual.

You can see where this leads perhaps, the designers were worried about having lots of types with big method tables. So looked for ways to cut down on the overhead.

Array.Sort() was an obvious target.

The same issue is not relevant for generic types. A big nicety of generics, one of many, one method table can handle the method pointers for any type parameter of a reference type.

like image 71
Hans Passant Avatar answered Oct 09 '22 18:10

Hans Passant


You are comparing two different types of 'object containers':

MyList is a generic collection of type List, a wrapper class, of type int, where the List<T> represents a strongly typed list of objects. The List class itself provides methods to search, sort, and manipulate its contained objects.

MyArray is a basic data structure of type Array. The Array does not provide the same rich set of methods as the List. Arrays can at the same time be single-dimensional, multidimensional or jagged, whilst Lists out of the box only are single-dimensional.

Take a look at this question, it provides a richer discussion about these data types: Array versus List<T>: When to use which?

like image 23
Avada Kedavra Avatar answered Oct 09 '22 18:10

Avada Kedavra