Arrays As Parameters (and Return Values)
Once you have created an array, you are free to pass it as a parameter and receive it as a member
return value. For example, the following PrintArray() method takes an incoming array of ints and
prints each member to the console, while the GetStringArray() method populates an array of
strings and returns it to the caller:
static void PrintArray(int[] myInts)
{
for(int i = 0; i < myInts.Length; i++)
Console.WriteLine("Item {0} is {1}", i, myInts[i]);
}
static string[] GetStringArray()
{
string[] theStrings = { "Hello", "from", "GetStringArray" };
return theStrings;
}
These methods may be invoked as you would expect:
C H A P T E R 4
■
C O R E C # P R O G R A M M I N G C O N S T R U C T S, PA RT I I
118
8849CH04.qxd 10/1/07 10:31 AM Page 118
static void PassAndReceiveArrays()
{
Console.WriteLine("=>Arrays as params and return values.");
Do'stlaringiz bilan baham: |