Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对C#和#x27感到困惑;s扩展方法重载解析_C#_Overloading_Overload Resolution - Fatal编程技术网

对C#和#x27感到困惑;s扩展方法重载解析

对C#和#x27感到困惑;s扩展方法重载解析,c#,overloading,overload-resolution,C#,Overloading,Overload Resolution,考虑以下代码: using System; using System.Linq; using System.Collections.Generic; public static class Ex { public static IEnumerable<T> Take<T>(this IEnumerable<T> source, long cnt) { return source; } } public class C

考虑以下代码:

using System;
using System.Linq;
using System.Collections.Generic;

public static class Ex
{
    public static IEnumerable<T> Take<T>(this IEnumerable<T> source, long cnt)
    {
        return source;
    }
}

public class C 
{
    public static void Main() 
    {
        foreach(var e in Enumerable.Range(0, 10).Take(5).ToArray())
            Console.Write(e + " ");
    }
}

尝试
System.Linq.Enumerable.Take(源代码,5)
而不是仅仅
Take(源代码,5)
强制使用原始的“Take”函数或将您自己的“Take”重命名为其他“Takef”以避免此类问题。

尝试
系统.Linq.Enumerable.Take(源代码,5)
而不是仅仅
Take(来源,5)
强制使用原始的“Take”函数,或将您自己的“Take”重命名为其他“Takef”以避免此类问题。

因为正如Eric Lippert所说:

判断一个潜在过载的基本规则 对于给定的呼叫站点,比另一个更好:越近越好 远比远


因为正如埃里克·利珀特所说:

判断一个潜在过载的基本规则 对于给定的呼叫站点,比另一个更好:越近越好 远比远



通过阅读
linq
.Take
上的文档,您不需要从
IEnumerable
中提取超过20亿项。因此,框架不提供
Take(long)的原因是
。你试过用
5L
而不是
5
调用它吗?我想因为你的扩展方法和调用方都在同一个名称空间中,那一个就赢了。现在记不起确切的规则了。@Sean,我调用
Take
长版本没有问题,这实际上是我的问题。@Blindy,你是re-example没有实际应用程序,将被视为糟糕的设计。无论您是否征求建议,我都在指出您示例中的错误。通过阅读
linq
.Take
,您不需要从
IEnumerable
中提取超过20亿项。因此,fram作业不提供
.Take(长)
。你试过用
5L
而不是
5
调用它吗?我想因为你的扩展方法和调用方都在同一个名称空间中,那一个就赢了。现在记不起确切的规则了。@Sean,我调用
Take
长版本没有问题,这实际上是我的问题。@Blindy,你是re-example没有实际应用程序,将被视为糟糕的设计。无论你是否征求建议,我都会指出你示例中的错误。这是一种扩展方法,你的建议毫无意义。我已经遇到了这个问题,我不知道为什么,但它有帮助(我知道在正常情况下这样做是没有意义的,但如果它起作用…^^'7)您可以将扩展方法作为正常的“前缀”方法调用,而不是作为“中缀”扩展方法调用。因此
System.Linq.Enumerable.Take(someCollection,5);
这是一种扩展方法,你的建议毫无意义。我已经遇到了这个问题,我不知道为什么,但它有帮助(我知道在正常情况下这样做是没有意义的,但如果它起作用的话…^^'7)你可以将扩展方法称为普通的“前缀”方法,而不是“中缀”方法扩展方法.So
System.Linq.Enumerable.Take(someCollection,5);
他列表中的最后一项将应用于我的第二个示例,因为嵌套的
using
是“closer”实际上,如果我在嵌套的名称空间中添加
using System.Linq
,它会像预期的那样工作。奇怪,但我只是问了原因,你是对的,就是这个,谢谢!他列表上的最后一项将应用于我的第二个示例,因为嵌套的
using
是“更接近”的对于我的用法来说,比外部的
System.Linq
。事实上,如果我在嵌套的名称空间中添加
using System.Linq
,它会像预期的那样工作。奇怪,但我只是问了原因,你说得对,就是这个,谢谢!
using System;
using System.Linq;
using System.Collections.Generic;

namespace N1
{
    public static class Ex
    {
        public static IEnumerable<T> Take<T>(this IEnumerable<T> source, long cnt)
        {
            return source;
        }
    }
}

namespace N2
{
    using N1;
    public class C 
    {
        public static void Main() 
        {
            foreach(var e in Enumerable.Range(0, 10).Take(5).ToArray())
                Console.Write(e + " ");
        }
    }
}