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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With