C#: Is it possible to return an IOrderedEnumerable

2020-04-05 07:35发布

问题:

Is it possible to return an IOrderedEnumerable<T> from a method without using the OrderBy or OrderByDescending methods on an IEnumerable<T>?

Im guessing perhaps not... but... maybe I am wrong?


Reason: Mostly curiosity. It just kind of hit me when making this answer on returning digits in a number. And my method would return the digits in ascending order, according to their weight in the given number. So I thought it could be nice if they came out ordered in a way that the framework would recognize as ordered as well. Of course we can argue that from a common number point of view they were not ordered. But yeah... if they should or shouldn't wasn't the point here. Just if it was possible or not.

I guess I am also kind of implying a question (or am now at least) on if an IOrderedEnumerable<T> is more than just an IEnumerable<T> with a different name. Does it contain anything more? i know it has the ThenBy and ThenByDescending methods, but do they use anything inside the IOrderedEnumerable<T>, or is it just that they don't make sense to use directly on an IEnumerable<T>?

回答1:

Well, you can implement IOrderedEnumerable<T> yourself if you want too... it's not hugely hard, depending on what you need to do. The easiest option is just to call OrderBy/OrderByDescending though :)

Why are you interested?