是否LINQ"查询语法"支持鸭打字? [英] Does LINQ "Query Syntax" Support Duck Typing?

查看:170
本文介绍了是否LINQ"查询语法"支持鸭打字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于LINQ查询语法...

Regarding LINQ query syntax...

var foo = new List<int> { 1, 2 };

var boo = from n in foo
            where n > 1
            select n;

......我一直以为这是语法仅限于IEnumerable的操作。或者至少,直到我了解到的IQueryable。也许的IObservable为好。但最近,我注意到一个建议,即<一href="http://bartdesmet.net/blogs/bart/archive/2009/08/17/linq-to-ducks-bringing-back-the-duck-typed-foreach-statement-to-linq.aspx"相对=nofollow>查询语法是基于鸭打字。这个故事看起来并不非常有说服力的,直到我发现了一个专用于的 LINQ到任务的。 LINQ到任务看起来是完全依赖鸭打字与查询语法!

...I always thought this syntax was limited to operating on IEnumerable. Or at least until I learned about IQueryable. And perhaps IObservable as well. But I recently noticed a suggestion that query syntax is based on duck typing. That story didn't look terribly convincing, until I found a site that is dedicated to LINQ to Tasks. LINQ to Tasks looks like it is wholly dependent on duck typing with query syntax!

好了,这是怎么回事吗?使用查询语法鸭打字或没有?当我试试看自己,果然这个作品,似乎要证明它是所有关于鸭打字,而不是IEnumerable的:

Ok, what is going on here? Is query syntax using duck typing or not? When I give it a try myself, sure enough this works and appears to prove it's all about duck typing, and not IEnumerable:

public class Joker<T>
{
    public T Item;

    public Joker(T item)
    {
        Item = item;
    }
}

public static class JokerHelp
{

    public static T2 Select<T,T2>(this Joker<T> joke, Func<T,T2> call)
    {
        return call(joke.Item);
    }
}

var oof = new Joker<int>(5);
int foo = from a in oof
          select a;

如果鸭子类型是如何查询语法的工作,为的是明显的情况下,如果可能这个官方(MSDN)文档?或者有什么合理的文档?

If duck typing is how query syntax works, as is evidently the case, where might be official (MSDN) documentation about this? Or any reasonable documentation?

推荐答案

有在C#的编译器结构类型匹配,而不是名义类型匹配的一些功能。例子包括的foreach 循环,查询COM prehension的语法(选择其中, 等),以及等待 / 异步。对于所有这些功能,编译器实际上只是在寻找与特定的名称,而不是特定的接口或类的方法。

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.

这些功能不依赖于特定接口的原因是为了分离从.NET Framework实现尽可能多的语言。我想这会被认为是鸭打字的一种形式。

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.

埃里克利珀解释了功能和推理更彻底的<一个href="http://blogs.msdn.com/b/ericlippert/archive/2011/06/30/following-the-pattern.aspx">here.

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

我已经注意到了, MSDN文档是经常错误或不完整有关这些功能的。

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

这篇关于是否LINQ&QUOT;查询语法&QUOT;支持鸭打字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆