Dhairya Bhardwaj Dhairya Bhardwaj - 3 months ago 7
C# Question

How to use Array.Sort method in a Programmer defined partial Array class in c#?

Hey everyone I was trying to extended an

Array
with a user defined method (
MajorityElementOf
) in context of my Program. so I use the following code :

partial class Array{
public static object MajorityElementOf(int[] arr)
{
Array.Sort(arr);
//rest of the code
}
}


Compiling this is giving an error:


'Array' does not contain a definition for 'Sort';


It may be due to the
Sort
method is not defined in this part of
Array
. Can anyone tell me how to use
Array.Sort(arr)
method in this code ?

Answer

Your problem is your Array class takes precedence in effect hiding the other class. You can use the full namespace to get around this, for example:

System.Array.Sort(arr);