메소드의 배열을 메소드 인수로 전달하십시오.
다음이 배열 선언 및 초기화라고 가정해 보겠습니다.
MyArray app = new MyArray();
/* an int array with 5 elements */
int [] balance = new int[]{1000, 2, 3, 17, 50}; 이제 getAverage() 메서드를 호출하고 배열을 메서드 인수로 전달합니다.
double getAverage(int[] arr, int size) {
// code
} 다음은 C#에서 메서드에 배열을 전달하는 방법을 보여주는 예제입니다.
예시
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();
/* an int array with 5 elements */
int [] balance = new int[]{1000, 2, 3, 17, 50};
double avg;
/* pass pointer to the array as an argument */
avg = app.getAverage(balance, 5 ) ;
/* output the returned value */
Console.WriteLine( "Average value is: {0} ", avg );
Console.ReadKey();
}
}
} 출력
Average value is: 214.4