Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.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# 具有2个数组作为输入参数和2个数组作为输出的函数?_C#_Arrays - Fatal编程技术网

C# 具有2个数组作为输入参数和2个数组作为输出的函数?

C# 具有2个数组作为输入参数和2个数组作为输出的函数?,c#,arrays,C#,Arrays,我可以创建一个函数C吗?它有两个数组作为输入参数,两个数组作为输出?您可以将两个数组作为参数传递给一个函数,但该函数可以返回一个值。因此,您可以使用需要返回的两个数组创建一个对象,然后返回该对象。一个方法只能有一个返回值;但是,您可以使用out参数返回多个结果 void MyFunction(int[] input1, int[] input2, out int[] output1, out int[] output2) 使用元组: public Tuple<Object[], Obje

我可以创建一个函数C吗?它有两个数组作为输入参数,两个数组作为输出?

您可以将两个数组作为参数传递给一个函数,但该函数可以返回一个值。因此,您可以使用需要返回的两个数组创建一个对象,然后返回该对象。

一个方法只能有一个返回值;但是,您可以使用
out
参数返回多个结果

void MyFunction(int[] input1, int[] input2, out int[] output1, out int[] output2)
使用元组:

public Tuple<Object[], Object[]> FunctionName(Object[] array1, Object[] array2)
{
    //Your code

    return new Tuple<Object[],Object[]>({},{});
}
公共元组函数名(对象[]数组1,对象[]数组2)
{
//你的代码
返回新元组({},{});
}
你当然可以,伙计

public ArrayGroup MyFunction(myType[] firstArg, myType[] secondArg) {
    ArrayGroup result = new ArrayGroup();
    /*Do things which fill result.ArrayOne and result.ArrayTwo */
    return ArrayGroup;
}

class ArrayGroup {
    myType[] ArrayOne { get; set;}
    myType[] ArrayTwo { get; set;}
}

用你想要的数组类型填写myType!像
string
int
或复杂类型

当然可以,从响应的容器开始:

void YourFunc(Int32[] input1, Int32[] input2, out Int32[] output1, out Int32[] output2)
{
   output1 = new Int32[] { 1, 2, 3 };
   output2 = new Int32[] { 4, 5, 6 };
}

…

YourFunc(i1, i2, out o1, out o2);
public class Response
{
    public string[] One{get;set;}
    public string[] Two{get;set;}
}
你的方法可能看起来像

public Response DoSomething(string[] inputOne, string[] inputTwo)
{
      // do some thing interesting 

      return new Respponse
      {
            One = new string[]{"Hello","World"},
            Two = new string[]{"Goodbye","Cruel","World"},
      }
}

选项一:创建一个保存结果的类型:

SomeResult MyFunction(T[] arr1, T[] arr2) 
{
   // ..
   return new SomeResult(r1, r2);
}

class SomeResult
{
   public SomeResult(T[] a, T[] b) { /* .. */ }

   // Rest of implementation...
}
选项二:返回元组:

Tuple<T[], T[]> MyFunction(T[] arr1, T[] arr2) { }
我更喜欢选项一,建议不要使用out参数。如果两个参数的类型相同,但不可互换,我建议也为参数创建一个类型,使其成为具有单个结果的单参数函数。

是的,您可以这样做

您需要传递两个输出数组作为对函数的引用

下面是代码示例

功能

private bool ArrayImp(string[] pArray1, string[] pArray2, ref string[] oArray1, ref string oArray2)
{
    //Do your work here
    //Here oArray1 and oArray2 are passed by reference so you can directly fill them
    // and get back as a output.
}
函数调用

string[] iArray1 = null;
string[] iArray2 = null;
string[] oArray1 = null;
string[] oArray2 = null;

ArrayImp(iArray1, iArray2, oArray1, oArray2);
在这里,您需要将iArray1和iArray2作为输入数组传递,然后将oArray1和oArray2作为输出


奶酪!!快乐编码

欢迎来到stackoverflow,我已经编辑了您的问题,请查看原因:
string[] iArray1 = null;
string[] iArray2 = null;
string[] oArray1 = null;
string[] oArray2 = null;

ArrayImp(iArray1, iArray2, oArray1, oArray2);