이것은 메소드에 매개변수를 전달하는 기본 메커니즘입니다. 이 메커니즘에서 메소드가 호출되면 각 값 매개변수에 대해 새 저장 위치가 생성됩니다.
실제 매개변수의 값이 복사됩니다. 따라서 메서드 내부의 매개변수에 대한 변경 사항은 인수에 영향을 미치지 않습니다. 다음은 값으로 매개변수를 전달하는 코드입니다.
예시
using System;
namespace CalculatorApplication {
class NumberManipulator {
public void swap(int x, int y) {
int temp;
temp = x; /* save the value of x */
x = y; /* put y into x */
y = temp; /* put temp into y */
}
static void Main(string[] args) {
NumberManipulator n = new NumberManipulator();
/* local variable definition */
int a = 100;
int b = 200;
Console.WriteLine("Before swap, value of a : {0}", a);
Console.WriteLine("Before swap, value of b : {0}", b);
/* calling a function to swap the values */
n.swap(a, b);
Console.WriteLine("After swap, value of a : {0}", a);
Console.WriteLine("After swap, value of b : {0}", b);
Console.ReadLine();
}
}
} 출력
Before swap, value of a : 100 Before swap, value of b : 200 After swap, value of a : 100 After swap, value of b : 200