C # pass an array to a function
In C #, you can pass an array as a parameter. You may not be indexed by specifying the name of an array to pass a pointer to the array to a function.
Examples
The following example demonstrates how to pass an array to a function:
using System; namespace ArrayApplication { class MyArray { double getAverage (int [] arr, int size) { int i; double avg; int sum = 0; for (i = 0; i <size; ++ i) { sum + = arr [i]; } avg = (double) sum / size; return avg; } static void Main (string [] args) { MyArray app = new MyArray (); / * A int array with five elements * / int [] balance = new int [] {1000, 2, 3, 17, 50}; double avg; Pointer / * array is passed as a parameter * / avg = app.getAverage (balance, 5); / * Output return value * / Console.WriteLine ( "average is: {0}", avg); Console.ReadKey (); } } }
When the above code is compiled and executed, it produces the following results:
Average is: 214.4