Why can't I call OrderBy in a class that extends List?
.net, c#, extension-methods, inheritance, linq
Solution
Add `this` to the front of `OrderBy` as in
this.OrderBy(a => Guid.NewGuid()); // a random ordering
`OrderBy` is an extension method on `IEnumerable<T>` and is not a public method on `List<T>`. If you type `OrderBy` with no context the compiler will look for an instance or static method named `OrderBy`. It is only if you prefix `OrderBy` with an instance of `IEnumerable<T>` will the compiler find `OrderBy`. As `Deck : List<Card>` and `List<Card> : IEnumerable<Card>`, using the keyword `this` (a reference to the current instance) will give the compiler the context it needs to locate the method `Enumerable.OrderBy`.
It is considered bad practice to inherit from `List<T>` in a public API. First, `List<T>` was not designed for inheritance and probably should have been `sealed`; too late for that now. In general, you should favor composition over inheritance when using framework classes.
Problem
I have a class, `Deck`, that contains a method called `Shuffle`. I'm working on refactoring `Deck` to extend `List<Card>`, rather than having `List<Card> Cards` as a property. However, while `Cards.OrderBy (a => Guid.NewGuid ())` worked, `OrderBy (a => Guid.NewGuid ())` does not: `Error CS0103: The name 'OrderBy' does not exist in the current context (CS0103)` Why does this not work?