Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I want to know how to pass two different arrays to one function.

Thanks
Posted
Updated 3-Oct-11 4:16am
v2

If you mean how to pass two arrays to a method, assuming your method signature is as follows:

C#
public void SampleMethod(string[] a, string[] b)
{

}


You could do it a bit like this:

C#
var array1 = new [] {"adasd", "asdasd"};

var array2 = new [] {"werewr", "werewr"};

SampleMethod(array1, array2);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Oct-11 19:18pm    
That's it, my 5.
--SA
jim lahey 4-Oct-11 3:19am    
Thanks :)
Simon Bang Terkildsen 4-Oct-11 2:00am    
My 5.
Though I want to express my hate for var :) Though in this case it's perfectly fine as it's very clear that the two arrays are string arrays.
jim lahey 4-Oct-11 3:21am    
Thanks, although I'm not about to ditch the var - I'm vary much a convert :) har har
Simon Bang Terkildsen 4-Oct-11 3:26am    
My problem with var is that it hurts readability, e.g. have a look at this
var data = GetData();
Can you tell me what data is? It could be a DataSet a collection, a string, well anything.
But as I said in the code you posted it's perfectly clear what the type is and thus fine to use var.
C#
public void MethodPassingTwoDifferentArrays()
{
    int[] myFirstArray = new int[0];
    int[] mySecondArray = new int[100];

    MethodAcceptingTwoArrays(myFirstArray, mySecondArray )
}

public void MethodAcceptingTwoArrays(int[] arr1, int[] arr2)
{
    // Play with the arrays, but no dirty business!
}
 
Share this answer
 
Comments
Abhinav S 3-Oct-11 10:18am    
Can't be simpler. My 5.
Simon Bang Terkildsen 3-Oct-11 11:28am    
Thank you, Abhinav
Sander Rossel 3-Oct-11 11:52am    
That's it, my 5.
And no algorithms involved ;p
Simon Bang Terkildsen 4-Oct-11 1:51am    
Yeah no algorithms :P
Espen Harlinn 3-Oct-11 17:51pm    
Nice :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900