8

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

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • possible duplicate of [Can a C# anonymous class implement an interface?](http://stackoverflow.com/questions/191013/can-a-c-sharp-anonymous-class-implement-an-interface) – nawfal Apr 19 '13 at 06:36

4 Answers4

9

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks; that's basically what I was wondering. I gave this +1 but accepted Will's answer since he actually went to the trouble of looking at the source code (in Reflector, I'm assuming). – Dan Tao Nov 26 '10 at 17:38
  • The source code is downloadable from MS (though their reference source project is a shameful shambles) - I have it indexed by Windows too, so it's very quick to look this sort of stuff up. http://referencesource.microsoft.com/netframework.aspx – Will Dean Nov 26 '10 at 18:02
7

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.

Will Dean
  • 39,055
  • 11
  • 90
  • 118
4

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.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
3

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

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • 4
    Chuck Norris knows the name of the anonymous type. – Tim Robinson Nov 26 '10 at 17:33
  • 1
    Right, but I was unsure if *this* is what the documentation even meant by "anonymous" (the compiler-generated type provided by, e.g., C#'s anonymous types feature). Turns out, from Will's answer, it's just a different meaning altogether. – Dan Tao Nov 26 '10 at 17:39