为什么是有用的访问静态成员"通过"继承类型? [英] Why is it useful to access static members "through" inherited types?

查看:100
本文介绍了为什么是有用的访问静态成员"通过"继承类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很高兴C#不会让你访问静态成员,就好像他们是实例成员。这样就避免了在Java中一个共同的错误:

I'm glad C# doesn't let you access static members 'as though' they were instance members. This avoids a common bug in Java:

Thread t = new Thread(..);
t.sleep(..); //Probably doesn't do what the programmer intended.

在另一方面,它的确实的让你通过访问静态成员派生类型。除了运营商(它可以节省你写的演员),我想不出任何情况下,这实际上是有帮助的。事实上,它积极鼓励失误,例如:

On the other hand, it does let you access static members 'through' derived types. Other than operators (where it saves you from writing casts), I can't think of any cases where this is actually helpful. In fact, it actively encourages mistakes such as:

// Nasty surprises ahead - won't throw; does something unintended:
// Creates a HttpWebRequest instead.
var ftpRequest = FtpWebRequest.Create(@"http://www.stackoverflow.com");

// Something seriously wrong here.
var areRefEqual = Dictionary<string, int>.ReferenceEquals(dict1, dict2);



我个人保持一遍又一遍犯类似的错误,当我通过陌生的API搜索我的方式(我记得与表达式树出发。我打 BinaryExpression 在编辑器中,并想知道为什么地球上的IntelliSense是为我提供 MakeUnary 。作为一个选项)

I personally keep committing similar errors over and over when I am searching my way through unfamiliar APIs (I remember starting off with expression trees; I hit BinaryExpression. in the editor and was wondering why on earth IntelliSense was offering me MakeUnary as an option).

在我的(短视)看来,这样的特征:

In my (shortsighted) opinion, this feature:


  1. 不减少赘言;程序员指定类型名称这种或那种方式(不包括运营商和情况下,当用户在访问继承了当前类型的静态成员)。

  2. 鼓励错误/误导性代码,如上面的那个。

  3. 可能会建议程序员在C#中展出的静态方法某种形式的多态的,当他们没有。

  4. (次要)推出沉默,可能无意重新绑定上重新编译的可能性。

  1. Doesn't reduce verbosity; the programmer has to specify a type-name one way or another (excluding operators and cases when one is accessing inherited static members of the current type).
  2. Encourages bugs/ misleading code such as the one above.
  3. May suggest to the programmer that static methods in C# exhibit some sort of 'polymorphism', when they don't.
  4. (Minor) Introduces 'silent', possibly unintended rebinding possibilities on recompilation.

(国际海事组织,运营商是保证自己讨论的一个特例。)

(IMO, operators are a special case that warrant their own discussion.)

由于C#是正常的语言成功的坑,为什么这个功能存在吗?我看不到它的好处(不是可发现等,这总是可以在IDE中解决),但我看到很多问题。

Given that C# is normally a "pit of success" language, why does this feature exist? I can't see its benefits (other than 'discoverability', which could always be solved in the IDE), but I see lots of problems.

推荐答案

我同意这是一个不好的特性。我不知道该怎么经常会有人在Stack Overflow上已经发布的代码:

I'd agree this is a misfeature. I don't know how often someone on Stack Overflow has posted code of:

ASCIIEncoding.ASCII

等等......这,而在执行的是在阅读代码术语误导性方面无害。

etc... which, while harmless in terms of execution is misleading in terms of reading the code.

显然为时已晚,现在去掉这个功能,虽然我猜C#团队可以引入一个超级详细的警告模式,这和其他样式的问题。

Obviously it's too late to remove this "feature" now, although I guess the C# team could introduce a super-verbose warning mode for this and other style issues.

也许C#的继任者将改善的事情...

Maybe C#'s successor will improve things...

这篇关于为什么是有用的访问静态成员&QUOT;通过&QUOT;继承类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆