C# Equivalent of swap in C++

I have here a piece of code that swap two numbers. whether its a string or and integer.

I would like to ask if theres an equivalent code for c#? If yes, then how can I do that?

Thank you

// This method will swap any two items.
// as specified by the type parameter <T>.

static void Swap<T>(ref T a, ref T b)
{
Console.WriteLine("You sent the Swap() method a {0}",
typeof(T));
T temp;
temp = a;
a = b;
b = temp;
}

static void Main(string[] args)
{
Console.WriteLine("***** Fun with Generics *****\n");
// Swap 2 ints.
int a = 10, b = 90;
Console.WriteLine("Before swap: {0}, {1}", a, b);
Swap<int>(ref a, ref b);
Console.WriteLine("After swap: {0}, {1}", a, b);
Console.WriteLine();

// Swap 2 strings.
string s1 = "Hello", s2 = "There";
Console.WriteLine("Before swap: {0} {1}!", s1, s2);
Swap<string>(ref s1, ref s2);
Console.WriteLine("After swap: {0} {1}!", s1, s2);
Console.ReadLine();
}
[1122 byte] By [pre_wreck] at [2007-11-20 11:08:41]
# 1 Re: C# Equivalent of swap in C++
I have here a piece of code that swap two numbers. whether its a string or and integer.

I would like to ask if theres an equivalent code for c#? If yes, then how can I do that?

Thank you

// This method will swap any two items.
// as specified by the type parameter <T>.

static void Swap<T>(ref T a, ref T b)
{
Console.WriteLine("You sent the Swap() method a {0}",
typeof(T));
T temp;
temp = a;
a = b;
b = temp;
}

static void Main(string[] args)
{
Console.WriteLine("***** Fun with Generics *****\n");
// Swap 2 ints.
int a = 10, b = 90;
Console.WriteLine("Before swap: {0}, {1}", a, b);
Swap<int>(ref a, ref b);
Console.WriteLine("After swap: {0}, {1}", a, b);
Console.WriteLine();

// Swap 2 strings.
string s1 = "Hello", s2 = "There";
Console.WriteLine("Before swap: {0} {1}!", s1, s2);
Swap<string>(ref s1, ref s2);
Console.WriteLine("After swap: {0} {1}!", s1, s2);
Console.ReadLine();
}
:thumb:

where you somehow under the impression that what you posted was not C#?
MadHatter at 2007-11-9 11:36:25 >
# 2 Re: C# Equivalent of swap in C++
:thumb:

where you somehow under the impression that what you posted was not C#?

What do you mean by this?

Yes I understand that this is not a c# code. All I wanna know if theres a similar construct in C# codes just like the generic template for c++? If Yes, then show it!

Thats all.
pre_wreck at 2007-11-9 11:37:27 >
# 3 Re: C# Equivalent of swap in C++
What he means is that the code you posted IS C# code.
jmcilhinney at 2007-11-9 11:38:26 >