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();
4 个解决方案
#1
4
Thanks to Pedro d'Aquino for identifying these other questions that provide answers.
感谢Pedro d'Aquino确定提供答案的其他问题。
The basic point is that instance methods on structures are not thread-safe but static methods are.
基本点是结构上的实例方法不是线程安全的,而是静态方法。
See these questions:
看到这些问题:
- Why is .NETs char.IsLower() a static method?
- 为什么.NETs char.IsLower()是静态方法?
- Why IsNan() is a static method on the double class instead of an instance property?
- 为什么IsNan()是double类的静态方法而不是实例属性?
#2
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
(编辑)我对Java很困惑
(关于静态成员):
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.
线程安全的观点也是一个很好的理由。
#3
1
You can do it for yourself if you use .NET 3.0, using extension methods:
如果您使用.NET 3.0,则可以使用扩展方法自行完成:
public static class Extensions
{
public static bool IsLetter(this chr)
{
return char.IsLetter(chr);
}
}
then call it like: c.IsLetter()
然后称它为:c.IsLetter()
Or do the way you want it. The same at sorting
或者按照你想要的方式做。排序时也一样
#4
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.
任何实现iComparable的类都可以放入数组并进行排序。如果它是数组的方法,那么我将不得不为我的自定义类型编写一个新的数组类型。
Also, as others noted, primitive types require this design of an array.
此外,正如其他人所指出的,原始类型需要这种数组设计。
#1
4
Thanks to Pedro d'Aquino for identifying these other questions that provide answers.
感谢Pedro d'Aquino确定提供答案的其他问题。
The basic point is that instance methods on structures are not thread-safe but static methods are.
基本点是结构上的实例方法不是线程安全的,而是静态方法。
See these questions:
看到这些问题:
- Why is .NETs char.IsLower() a static method?
- 为什么.NETs char.IsLower()是静态方法?
- Why IsNan() is a static method on the double class instead of an instance property?
- 为什么IsNan()是double类的静态方法而不是实例属性?
#2
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
(编辑)我对Java很困惑
(关于静态成员):
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.
线程安全的观点也是一个很好的理由。
#3
1
You can do it for yourself if you use .NET 3.0, using extension methods:
如果您使用.NET 3.0,则可以使用扩展方法自行完成:
public static class Extensions
{
public static bool IsLetter(this chr)
{
return char.IsLetter(chr);
}
}
then call it like: c.IsLetter()
然后称它为:c.IsLetter()
Or do the way you want it. The same at sorting
或者按照你想要的方式做。排序时也一样
#4
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.
任何实现iComparable的类都可以放入数组并进行排序。如果它是数组的方法,那么我将不得不为我的自定义类型编写一个新的数组类型。
Also, as others noted, primitive types require this design of an array.
此外,正如其他人所指出的,原始类型需要这种数组设计。