方法1:
using System;
using System.Linq;
using System.Text.RegularExpressions;
class Program
{
public static void Main()
{
int a = -5;
int b = 3;
Swap(ref a, ref b);
Console.WriteLine("a={0},b={1}",a,b);
}
public static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
}
using System.Linq;
using System.Text.RegularExpressions;
class Program
{
public static void Main()
{
int a = -5;
int b = 3;
Swap(ref a, ref b);
Console.WriteLine("a={0},b={1}",a,b);
}
public static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
}
方法2:
using System;
using System.Linq;
using System.Text.RegularExpressions;
class Program
{
public static void Main()
{
int a = -5;
int b = 3;
Swap(ref a, ref b);
Console.WriteLine("a={0},b={1}",a,b);
}
public static void Swap(ref int a, ref int b)
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
}
using System.Linq;
using System.Text.RegularExpressions;
class Program
{
public static void Main()
{
int a = -5;
int b = 3;
Swap(ref a, ref b);
Console.WriteLine("a={0},b={1}",a,b);
}
public static void Swap(ref int a, ref int b)
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
}
方法3:
using System;
using System.Linq;
using System.Text.RegularExpressions;
class Program
{
public static void Main()
{
int a = -5;
int b = 3;
Swap(ref a, ref b);
Console.WriteLine("a={0},b={1}",a,b);
}
public static void Swap(ref int a, ref int b)
{
a = a + b;
b = a - b;
a = a - b;
}
}
using System.Linq;
using System.Text.RegularExpressions;
class Program
{
public static void Main()
{
int a = -5;
int b = 3;
Swap(ref a, ref b);
Console.WriteLine("a={0},b={1}",a,b);
}
public static void Swap(ref int a, ref int b)
{
a = a + b;
b = a - b;
a = a - b;
}
}
浙公网安备 33010602011771号