Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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#中的数组?_C# - Fatal编程技术网

如何重载函数,使其可以接受c#中的数组?

如何重载函数,使其可以接受c#中的数组?,c#,C#,我写了一个函数,它似乎适用于两个值(intr,intx),但现在我需要重载它,并使用一个数组来代替(intr,array)。我该怎么办?数组的大小在这里不太重要,它需要一些小的数字,比如82764等等 static double rthRoot (double r, double x) { double y = double.NaN; if (x > 0) { y = Math.Exp((Math.Log

我写了一个函数,它似乎适用于两个值(intr,intx),但现在我需要重载它,并使用一个数组来代替(intr,array)。我该怎么办?数组的大小在这里不太重要,它需要一些小的数字,比如82764等等

  static double rthRoot (double r, double x)
    {

        double y = double.NaN;
        if (x > 0)
        {
            y = Math.Exp((Math.Log(x)) / r);
        }

        if (r % 2 != 0)
        {
            if (x < 0)
            {
                y = -(Math.Exp((Math.Log(Math.Abs(x))) / r));
            }
        }

        return y;

    }int[] myArray;
        myArray = new int[] { 8, 27, 64, 125 };
静态双根(双r,双x)
{
双y=双NaN;
如果(x>0)
{
y=Math.Exp((Math.Log(x))/r);
}
如果(r%2!=0)
{
if(x<0)
{
y=-(Math.Exp((Math.Log(Math.Abs(x)))/r));
}
}
返回y;
}int[]myArray;
myArray=newint[]{8,27,64,125};

我想重载这个函数,但是我想使用一个数组,比如末尾的数组,而不是双x

    static double rthRoot(double r, double x) //1
    {
        //original
        double y = double.NaN;
        if (x > 0)
        {
            y = Math.Exp((Math.Log(x)) / r);
        }

        if (r % 2 != 0)
        {
            if (x < 0)
            {
                y = -(Math.Exp((Math.Log(Math.Abs(x))) / r));
            }
        }

        return y;

    }
    static double rthRoot(double r, int[] x) //2
    {
        //overload
        double y = double.NaN;
        //whatever

        return y;

    }
静态双根(双r,双x)//1
{
//原创的
双y=双NaN;
如果(x>0)
{
y=Math.Exp((Math.Log(x))/r);
}
如果(r%2!=0)
{
if(x<0)
{
y=-(Math.Exp((Math.Log(Math.Abs(x)))/r));
}
}
返回y;
}
静态双根(双r,int[]x)//2
{
//超载
双y=双NaN;
//随便
返回y;
}

如果您为第二个参数传递一个
int[]
,它将转到方法2。

这就是您想要的类型吗

static double[] rthRoot(double r, double[] xs)
{
    return xs.Select(x => rthRoot(r, x)).ToArray();
}

static double[] rthRoot(double r, int[] xs)
{
    return xs.Select(x => rthRoot(r, (double)x)).ToArray();
}

两者都依赖于您当前对
rthRoot
的定义来工作。

您的问题不清楚。数组参数与任何其他类型的参数没有任何区别。您仍在向该方法传递一个值,并且您的参数列表指示该类型。重载对数组参数的作用与对任何其他类型的参数的作用相同。编辑你的问题,使其包含一个好的答案,清楚地显示你已经尝试了什么,并解释你在工作中遇到的具体困难。请确保包含任何错误或异常的确切文本,对于异常,请包含完整的堆栈跟踪。确保你的问题也解释了你到目前为止所做的研究。不清楚你想做什么。您是否试图重载返回值?