为什么要约束泛型扩展方法不被识别为扩展方法? [英] Why generic extension method with constraint is not recognized as extension method?

查看:113
本文介绍了为什么要约束泛型扩展方法不被识别为扩展方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
没有使用通用的扩展方法的类型推断

考虑两种方法:

public static IEnumerable<V> Merge<V>
       (this IEnumerable<IEnumerable<V>> coll)

public static IEnumerable<V> Merge<T, V>
       (this IEnumerable<T> coll) 
              where T : IEnumerable<V>



这两种编译就好了,在这两种情况下,泛型类型的类型将在主叫编译时是已知,因此,确切类型扩展类型。

Both compile just fine, in both cases the type of generic types will be known at compile time of caller, and thus the exact type of extended type.

您可以打电话都很好,但只有第一个为扩展名。

You can call both fine, but only the first one as extension.

为什么?

要看到它失败,使用第二种方法和这样的例子

To see it fail, use the second method and such example:

    var x = new List<List<int>>();
    var y = x.Merge();



更新 - 关闭



别T优人认为原来的岗位是太复杂的,以获得清晰的照片?教育目的,我认为这个职位不应该被关闭,即使在技术上(即答案)是重复的。只是我的2美分。

Update -- closing

Don't you guys think the original post is WAY too elaborate to get the clear picture? For educational purposes I think this post shouldn't be closed, even if technically (i.e. the answer) it is duplicate. Just my 2 cents.

推荐答案

进行推论,当方法类型推断没有考虑约束条件考虑在内。

Method type inference does not take constraints into account when making inferences.

这同样的问题,昨天问。见我的回答有更多的细节。

This same question was asked yesterday. See my answer there for more details.

没有使用通用扩展方法

这篇关于为什么要约束泛型扩展方法不被识别为扩展方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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