Can an anonymous type inherit from another type?

2019-06-16 10:52发布

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"?

4条回答
小情绪 Triste *
2楼-- · 2019-06-16 11:09

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-06-16 11:20

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.

查看更多
虎瘦雄心在
4楼-- · 2019-06-16 11:23

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.

查看更多
再贱就再见
5楼-- · 2019-06-16 11:27

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...

查看更多
登录 后发表回答