为什么DOTNET的char.IsLower()的静态方法?静态、方法、DOTNET、char

2023-09-03 16:38:18 作者:耳朵在听

这似乎违背了每一个设计方针。 接受单个T类型的参数静态方法通常应该只是一个成员方法。

This seems to go against every design guideline. A static method that accepts a single argument of type T should usually just be a member method.

它是如此的bizzare其实我不得不发布StackOverflow问题了解IsUpper存在(因为它没有自动完成显示)

It's so bizzare I actually had to post a StackOverflow question to understand IsUpper exists (as it didn't show up in auto-completion)

修改

我明白我刚才的发言需要一点解释。的一个良好的设计的一个例子是String.ToLower()。相反,它被作为原型静态无效TOLOWER(字符串FOO),它是一个成员方法。这是pretty的明显(至少对我来说),这同样应按住char.IsLower()。

I understand my earlier statement needs a little explaining. An example of a good design is String.ToLower(). Instead of it being prototyped as static void ToLower(String foo), it is a member method. It's pretty obvious (to me at least) that the same should hold for char.IsLower().

推荐答案

在结构实例方法的不是线程安全的。 在另一方面静态方法是

Instance methods on structures are not thread safe. Static methods on the other hand are.

静态方法获得结构的副本,实例方法有管理的指针。通过指针访问数据不是胎面操作安全,很容易导致竞争条件。

Static methods receive a copy of the structure, instance methods a managed pointer. Accessing data through a pointer is not a tread safe operation and can easily lead to race conditions.

这就是为什么在结构/图元大部分方法是静态的,而不是实例。

That's why most methods on structures/primitives are static and not instance.

硒这里类似的问题。

Why IsNan是Double类的静态方法,而不是一个实例属性?