Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting from Array class

Tags:

arrays

c#

.net

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.

like image 881
Joe Avatar asked Dec 04 '22 10:12

Joe


1 Answers

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.

like image 183
Darin Dimitrov Avatar answered Dec 06 '22 00:12

Darin Dimitrov