Question

Sorry for what is probably a silly question but it's bugging me...

int[] i = {3, 2, 1};
//why
Array.Sort(i);
//instead of
i.Sort();

char c = 'c';
//why
char.IsLetter(c);
//instead of
c.Isletter();
Was it helpful?

Solution

Thanks to Pedro d'Aquino for identifying these other questions that provide answers.

The basic point is that instance methods on structures are not thread-safe but static methods are.

See these questions:

OTHER TIPS

These are utility methods that don't need to belong to these classes. This reinforces the Single Responsibility Principle

(edit) I was confusing with Java

(About static members):

Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.

The thread-safe point of view is also a good reason.

You can do it for yourself if you use .NET 3.0, using extension methods:

public static class Extensions
{
public static bool IsLetter(this chr)
{
 return char.IsLetter(chr);
}
}

then call it like: c.IsLetter()

Or do the way you want it. The same at sorting

It's an implementation decision. I don't know what all was going through the framework designer's heads, but I believe one reason is to allow array of custom types to be sorted with the least effort.

Any class that implements iComparable can be put into an array and sorted. If it was a method of the array, then i would have to write a new Array type for my custom type.

Also, as others noted, primitive types require this design of an array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top