REPOST: Array.Sort a Static method?

  • Thread starter Thread starter Rene \(Programmer Wannabe\)
  • Start date Start date
R

Rene \(Programmer Wannabe\)

Sorry for reposting this question, I asked this a couple of days ago but got
no definite answer as to why the Array class behaves this way. Hope this
time I will have better luck. Please read below:


Why do I have to resort to using the static method of the array like this:

int[] someArray = new int[3];
Array.Sort(someArray);

Why wasn't the Sort method for the array implemented like this:

int[] someArray = new int[3];
someArray.Sort();

There are other classes that behave the same way and just curios to know the
reason behind this. Thank you.
 
The Sort method of the Array class is only available as a static method, not
an instance method. If you are asking why the FCL implementers decided to
use a static method I believe the answer would be that it is more versatile,
but you would have to ask them.

Thomas P. Skinner [MVP]
 
int[] someArray = new int[3];
Array.Sort(someArray);

Why wasn't the Sort method for the array implemented like this:

int[] someArray = new int[3];
someArray.Sort();

I suspect this may have something to do with the way arrays actually work.
This isn't definitive as I didn't write the class, but it seems quite
logical.

For single dimension, zero based arrays, the runtime provides instructions
to manipulate them(ldelem*, stelem*, etc), whereas non zero based and
multi-dimensional arrays are used by calling methods on the Array
class(GetValue and SetValue, the compiler does this transformation for you).

Array.Sort is only capable of operating on single dimensional arrays(and
probably zero based as well) and therefore does not provide functinoality
that can be used in all arrays and does not apply as an Array instance
method.
There are other classes that behave the same way and just curios to know
the reason behind this. Thank you.

What other classes do you mean?

Some like Math make sense, because adding the entire set of methods to
simple numbers would be annoying(imagine int have 300 methods), I'm curious
about what others you mean.
 
Back
Top