匿名类型可以从另一个类型继承吗?
根据StringComparer.OrdinalIgnoreCase
属性:
OrdinalIgnoreCase 属性实际上返回派生自 的匿名类的实例StringComparer 类。
这是我不熟悉的功能吗——带有继承的匿名类型?或者作者所说的“匿名类”只是指“从 StringComparer
派生的内部类,对客户端代码不可见”?
According to the MSDN documentation on the StringComparer.OrdinalIgnoreCase
property:
The OrdinalIgnoreCase property actually returns an instance of an anonymous class derived from the StringComparer class.
Is this a feature I'm unfamiliar with—anonymous types with inheritance? Or by "anonymous class" did the author simply mean "internal class deriving from StringComparer
, not visible to client code"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它不是该术语正常 C# 含义中的匿名类型。
它只是一种内部类型,因此您不知道它的名称:您无法在代码中引用确切的类型。
It's not an anonymous type in the normal C# meaning of the term.
It's just a type which is internal, so you don't know the name of it: you can't refer to the exact type within your code.
如果您查看 StringComparer 的源代码,您可以看到 OrginalIgnoreCase 返回 OrdinalComparer 的实例,该实例派生自 StringComparer。
我看不到任何“匿名”的东西,只是它是内部的,所以你无法从框架外部看到它。
If you look at the source code for StringComparer, you can see that OrginalIgnoreCase returns an instance of OrdinalComparer, which is derived from StringComparer.
There's nothing 'anonymous' about this that I can see, it's just that it's internal so you can't see it from outside the framework.
编译器可以创建从其他类型继承的匿名类型 - 但你不能。这太糟糕了,实际上,因为动态创建继承另一个类或实现接口的匿名类型将是一个很酷的功能。
The compiler can create anonymous types that inherit from another type - you cannot. It's too bad, really as it would be a cool feature to create an anonymous type on the fly that either inherits from another class or implements an interface.
匿名类型对我们来说是匿名,而不是对 CLR 和编译器来说。编译器使用一个有趣的命名,其中包括 <>在名称中,只有编译器才能做到这一点!和也许查克·诺里斯...
Anonymous type is anonymous to us not the CLR and complier. Compiler uses a funny naming which includes <> in the name and only compiler can do that! and maybe Chuck Norris...