10

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();
Kev
  • 118,037
  • 53
  • 300
  • 385
paul
  • 101
  • 3
  • 2
    Have you got the right one? Post it. – Robert Harvey Jun 24 '09 at 13:51
  • 2
    This question is pretty similar to http://stackoverflow.com/questions/389257/why-is-dotnets-char-islower-a-static-method. – Pedro d'Aquino Jun 24 '09 at 13:54
  • 1
    @Robert: I'm not quite sure what the actual "right" answer is but I can identify the wrong answers ;). My guess is that it's mostly a design decision that was motivated by some factors I don't know. – VVS Jun 24 '09 at 14:06
  • @VVS: The Thomas Edison approach - finding 1000 ways that don't work. :D I like it. – Jeff Yates Jun 24 '09 at 14:14
  • @Pedro: Thanks for posting that. I've translated it into a community wiki'd answer. http://stackoverflow.com/questions/1038406/why-arent-array-methods-built-into-an-array-instance/1038662#1038662 – Jeff Yates Jun 24 '09 at 14:20

4 Answers4

4

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:

Community
  • 1
  • 1
Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
2

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.

Community
  • 1
  • 1
bruno conde
  • 47,767
  • 15
  • 98
  • 117
  • Thanks - still seems to me an array should be able to sort itself - if I wanted to code in c++ I would :) – paul Jun 24 '09 at 14:05
  • @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 Jun 24 '09 at 14:14
  • @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 Jun 24 '09 at 14:28
1

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
  • 1,909
  • 2
  • 22
  • 31
1

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
  • 12,316
  • 3
  • 41
  • 54
  • 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 Jun 24 '09 at 14:05
  • what type for each sub object would that instance method use? – Tim Hoolihan Jun 24 '09 at 14:16
  • @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 Jun 24 '09 at 14:16
  • @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 Jun 24 '09 at 14:19
  • @Tim: IComparable. What type do you think the static method uses? – Jeff Yates Jun 24 '09 at 14:22
  • And how do you implement iComparable on a Primitive type? – Tim Hoolihan Jun 24 '09 at 14:23
  • @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 Jun 24 '09 at 14:25
  • And to add to that, you can still provide your own Sort method, regardless of how the framework implements its own. – Jeff Yates Jun 24 '09 at 14:32
  • 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 Jun 24 '09 at 15:16
  • @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 Jun 24 '09 at 17:46
  • @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 Jun 25 '09 at 11:00