Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Writing a method in C# to realize the exchange of two integer data requires passing method parameters by reference (using ref). Please help me explain what it means.
Writing a method in C# to realize the exchange of two integer data requires passing method parameters by reference (using ref). Please help me explain what it means.

The answer is mainly because I saw that you have more points. . . So I wrote an example for you. There's not much to say, it's similar to pointers in C language. If ref is not added, the value is assigned when the function parameter is passed, and the operations inside the function will not affect the outside. Adding ref means it has the same value as the outside.

static?void?Main(string[]?args)

{

int?a?=?2;

int?b?=?3;

Console.WriteLine("a={0},b={1}",?a,?b);

Swap(ref?a,?ref?b) ;//?2,?3

Console.WriteLine("a={0},b={1}",?a,?b);

Console.ReadLine ();//?3,?2

}

static?void?Swap(ref?int?a,?ref?int?b)

{

int?c?=?a;

a?=?b;

b?=?c;

}