Does LINQ “Query Syntax” Support Duck Typing?

余生颓废 提交于 2019-12-01 14:13:08

There are a few features in C# that the compiler does structural type matching rather than nominal type matching. Examples include the foreach loop, query comprehension syntax (the select, where, etc), and await/async. For all of these features, the compiler is actually just looking for methods with certain names, not specific interfaces or classes.

The reason these features are not tied to specific interfaces is to decouple the language from the .NET framework implementation as much as possible. I suppose this would be considered a form of duck typing.

Eric Lippert explains the feature and reasoning much more thoroughly here.

I have noticed that the MSDN documentation is often wrong or incomplete about these features.

What you're missing is that List<T> implements IEnumerable<T>. Thus, "I always thought this syntax was limited to operating on IEnumerable" is technically true, though in a limited fashion. IQueryable implements IEnumerable as well, along with IList and arrays. Thus, you can perform linq queries against anything that implements IEnumerable.

Since Joker<> doesn't implement IEnumerable<>, your query attempt will fail. The Select<>(), Where<>(), etc. extension methods are built around IEnumerable<>. So, if you want to select from oof, you just need to update your definition of Joker<>

public class Joker<T> : IEnumerable<T>
{
  // (actually implement IEnumerable<T> functionality
}

(Edit: Answer did make some sense in the context of the originally-formatted question. Edited question makes my response obsolete)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!