tags:

views:

183

answers:

4

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();
+1  A: 

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

Timotei Dolean
+1  A: 

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.

Tim Hoolihan
I don't agree with your reasoning on if Sort was an instance method. It would still be able to work the way the static method does, without the need to write a custom array type.
Jeff Yates
what type for each sub object would that instance method use?
Tim Hoolihan
@Jeff Yates, but if you want change the implementation of sort, you DOES NOT need cahange the Array class, but you can add the static method anywhere else.But since LINQ ability in .net languages, there are extension methods, which looks like instance methods, but are static methods. Its pretty compromise I think.
TcKs
@TcKs: You can still provide IComparable implementations or your own sort - even if there is an instance method for Sort. I don't see what you're getting at.
Jeff Yates
@Tim: IComparable. What type do you think the static method uses?
Jeff Yates
And how do you implement iComparable on a Primitive type?
Tim Hoolihan
@Tim: What can the static method do that an instance method can't? I'm not sure what your point is but Sort already works on primitive types, so why wouldn't an instance version?! It would use the exact same mechanism.
Jeff Yates
And to add to that, you can still provide your own Sort method, regardless of how the framework implements its own.
Jeff Yates
primitive types can't implement interfaces. i don't know how else to explain it, but try writing an instance sort method that works with any type using only libraries available in .net 1.0. you can do it, but it gets ugly. suddenly your instance method is reflecting on types and performance goes way down.
Tim Hoolihan
@Tim: Why do they need to implement interfaces? What stance are you taking in your argument? Whether Sort is static or instance-based, the same "issue" that you describe occurs, which makes it moot in this discussion.
Jeff Yates
@Jeff: But the interface IComparable does not say anything about sorting-algorithm. You can use binary tree, buble sort, quick sort, etc...And - the objects can be sorted by diferent ways. You can sort byt "Name" or "Birth date" or anything else. For this ways is better use the IComparer interface. But it still say nothing about sort algorithm.
TcKs
+2  A: 

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.

bruno conde
Thanks - still seems to me an array should be able to sort itself - if I wanted to code in c++ I would :)
paul
@bruno well, but "isLetter" does depend on the object's identity, doesn't it? I mean, 'A'.isLetter() is surely different than '4'.isLetter().
Pedro d'Aquino
@Pedro, here, "the data and functions do not change regardless of what happens to the object". For "c.isLetter()" to exist, it would have to save an extra bool value in the char struct. The implementation would be different ...
bruno conde
+4  A: 

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:

Jeff Yates
Community wiki'd because this is none of my own work.
Jeff Yates