using System;
namespace Test
{
class Test
{
static void Main()
{
string s1, s2;
s1 = "foo";
s2 = "bar";
Console.WriteLine("s1: {0}, s2: {1}", s1, s2);
Swap(ref s1, ref s2);
Console.WriteLine("s1: {0}, s2: {1}", s1, s2);
int i1, i2;
i1 = 0;
i2 = 1;
Console.WriteLine("i1: {0}, i2: {1}", i1, i2);
Swap(ref i1, ref i2);
Console.WriteLine("i1: {0}, i2: {1}", i1, i2);
}
static void Swap<T>(ref T lhs, ref T rhs)
{
Console.WriteLine("call Swap<{0}>", lhs.GetType());
T temp = lhs;
lhs = rhs;
rhs = temp;
}
static void Swap(ref int lhs, ref int rhs)
{
Console.WriteLine("call Swap");
int temp = lhs;
lhs = rhs;
rhs = temp;
}
}
}
> csc try.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.
> try.exe
s1: foo, s2: bar
call Swap<System.String>
s1: bar, s2: foo
i1: 0, i2: 1
call Swap
i1: 1, i2: 0