Can I create a function C# which has 2 arrays as input parameter and 2 as output?
8 Answers
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
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
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
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!!