4

All arrays i create in C# inherit implicitly from the Array class. So why are methods like Sort() etc not available to the array i create. For example, consider the following code:

int [] arr = new int[]{1,2,3,4,5};

Console.WriteLine(arr.Length); //This works,Length property inherited from Array

Array.Sort(arr); //Works

arr.Sort(); //Incorrect !

Please Help Thank You.

2
  • There are loads of reasons... you cannot inherit an Array for example, what do you mean with 'implicitly' here? Commented Nov 19, 2010 at 13:44
  • Fall in the pit of success kinda feature, I think. An array does not know how to sort itself. Commented Nov 19, 2010 at 15:33

4 Answers 4

12

It's because the Sort method is a static method defined on the Array class so you need to call it like this:

Array.Sort(yourArray);

You don't need an instance of the class to call a static method. You directly call it on the class name. In OOP there's no notion of inheritance for static methods.

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

2 Comments

Thanks for the reply. So static methods are not inherited. Any particular reason for that ?.Also,can static data members be inherited ?
@Joe: It's not so much that they're not inherited as that you don't call them "on" an instance. In this case you happen to supply an instance to the method, but that's effectively a coincidence.
2

Sort is a static method on the Array class, that's why you call it using Array.Sort(). When you try arr.Sort(), you're trying to access an instance method which doesn't exist.

Comments

2

You could probably use an extension method to add a Sort function as an instance member of the Array type.

public static class ArrayExtension
{
    public static void Sort(this Array array)
    {
        Array.Sort(array);
    }
}

Example

int [] arr = new int[]{1,2,3,4,5};
arr.Sort();

DISCLAIMER: I have not compiled and tried this, but I am pretty sure that it will work.

1 Comment

I wonder why they haven't modified their Array.Sort signature in the first place when they where introducing ExtensionMethods. Old code using the static version still would compile and as it is syntactic sugar which the compiler handles, existing binaries shouldn't be affected. (Although this would mean adding an ArrayExtensions class or relaxing the "non-generic, non-nested, static class" constraint for Extensions)
1

Array.Sort() is a static method so you won't see it hanging off instances. However, you can use the OrderBy() extension method on any IEnumerable to do what you are seeking.

2 Comments

OrderBy doesn't sort an array internally; it creates a separate (but sorted) sequence. This is not the same thing.
@Marc - you are absolutely correct. I was just trying to give him an alternative to meet his goal after I explained the static method aspect. Thanks.

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.