C # How to exchange two variable values,
At the early stage of learning. Net/C # or any object-oriented language, everyone has written to exchange two variable values, usually through temporary variables. This article uses multiple methods to exchange two variable values.
Suppose int x = 1; int y = 2; now the values of the two variables are exchanged.
Use temporary variables
static void Main(string[] args)
{
int x = 1;
int y = 2;
Console.WriteLine("x={0},y={1}",x, y);
int temp = x;
x = y;
y = temp;
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadKey();
}
Use addition and subtraction
Imagine that 1 + 2 = 3, we get the result 3 of adding two numbers. 3-2 = 1, assign 1 to y, and y is equal to 1; 3-1 = 2, assign 2 to x, which completes the exchange.
static void Main(string[] args)
{
int x = 1;
int y = 2;
Console.WriteLine("x={0},y={1}",x, y);
x = x + y; //x = 3
y = x - y; //y = 1
x = x - y; //x = 2
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadKey();
}
Using ref and generic methods
If You encapsulate the algorithm for exchanging int type variable values into a method, you need to use the ref keyword.
static void Main(string[] args)
{
int x = 1;
int y = 2;
Console.WriteLine("x={0},y={1}",x, y);
Swap(ref x, ref y);
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadKey();
}
static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = x;
}
If you exchange variable values of the string type, you need to write a Swap method overload to receive the string type:
static void Main(string[] args)
{
string x = "hello";
string y = "world";
Console.WriteLine("x={0},y={1}",x, y);
Swap(ref x, ref y);
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadKey();
}
static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = x;
}
static void Swap(ref string x, ref string y)
{
string temp = x;
x = y;
y = x;
}
What if we exchange other types of variable values? We can easily think of implementation through the generic method, and then write another generic overload.
static void Main(string[] args)
{
string x = "hello";
string y = "world";
Console.WriteLine("x={0},y={1}",x, y);
Swap<string>(ref x, ref y)
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadKey();
}
static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = x;
}
static void Swap(ref string x, ref string y)
{
string temp = x;
x = y;
y = x;
}
static void Swap<T>(ref T x, ref T y)
{
T temp = x;
x = y;
y = temp;
}
Use bitwise OR Operators
For binary numbers, when two numbers are different, they are 1, that is, 0 and 1 are exclusive or the result is 1, 0, and 0, and the result of 1 and 1 is 0. Here an introduction to exclusive or equal operators: http://www.cnblogs.com/darrenji/p/3921183.html
For example, convert 3 and 4 in decimal format to 16-bit binary values:
X = 0000000000000011; // decimal number 3
Y = 0000000000000100; // decimal number 4
Assign the result of the XOR between x and y to x: x = x ^ y;
X = 0000000000000111;
Returns the XOR of y and current x to y: y = y ^ x.
Y = 0000000000000011;
Returns the XOR of the current x and current y to x: x = x ^ y.
X = 0000000000000100;
The preceding algorithm can be written as follows:
static void Main(string[] args)
{
int x = 1;
int y = 2;
Console.WriteLine("x={0},y={1}",x, y);
x = x ^ y;
y = y ^ x;
x = x ^ y;
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadKey();
}
"From the perspective of the ridge side, the distance is different." In the technology world, there may be no absolute simplicity or complexity. Then, complicated problems can be split into simple models; simply looking at the problem from a different perspective and implementing it in a different way gives us more fun.