3

Can I create a function C# which has 2 arrays as input parameter and 2 as output?

1

8 Answers 8

1

A method can only have one return value; however, you can use out parameters to return multiple results.

void MyFunction(int[] input1, int[] input2, out int[] output1, out int[] output2)
Sign up to request clarification or add additional context in comments.

Comments

1

Use Tuple :

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

    return new Tuple<Object[],Object[]>({},{});
}

Comments

0
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);

Comments

0

You sure can, buddy!

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;}
}

Fill in myType with whatever types you want the arrays to be! Like string or int or a complex type!

Comments

0

Sure you can, start with a container for the response:

public class Response
{
    public string[] One{get;set;}
    public string[] Two{get;set;}
}

And your method might look like

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"},
      }
}

Comments

0

Option one: Create a type holding the result:

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

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

   // Rest of implementation...
}

Option two: Return a tuple:

Tuple<T[], T[]> MyFunction(T[] arr1, T[] arr2) { }

Option three: Use out parameters (don't do this):

void MyFunction(T1[] arr1, T[] arr2, out T[] result1, out T[] result2) { }

I prefer option one, and recommend against using out parameters. If the two arguments are of the same type but not interchangeable I'd recommend also creating a type for the argument to make it a single argument function with a single result.

Comments

0

yes you can do this!

You need to pass two output arrays as a reference to the function.

Here is the code example.

Function

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.
}

Function Call

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

ArrayImp(iArray1, iArray2, oArray1, oArray2);

Here you need to pass iArray1 and iArray2 as input array and you will get oArray1 and oArray2 as output.

Cheeerss!! Happy Coding!!

Comments

0

You can pass 2 arrays as parameters to a function, but the function can return a single thing. So you could create an object with the two arrays you need to return, and return that object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.