我有這樣的結構如下。現在,我想交換2個結構。如何在c#中交換通用結構?
public struct Pair<T, U>
{
public readonly T Fst;
public readonly U Snd;
public Pair(T fst, U snd)
{
Fst = fst;
Snd = snd;
}
public override string ToString()
{
return "(" + Fst + ", " + Snd + ")";
}
**public Pair<U, T> Swap(out Pair<U, T> p1, Pair<T,U> p2)
{
p1 = new Pair<U, T>(p2.Snd, p2.Fst);
return p1;
}**
}
在Main方法試試這個:
Pair<int, String> t1 = new Pair<int, string>();
Pair<String, int> t2 = new Pair<string,int>("Anders",13);
**t1.Swap(out t1,);** //compilator tells -> http://i.stack.imgur.com/dM6P0.png
上交換方法的參數比compilator achive不同。
你不應該叫't2.Swap'嗎? – 2012-01-30 20:02:43
你是否收到編譯錯誤,或者它只是智能錯誤? – thumbmunkeys 2012-01-30 20:03:09
:)謝謝,我應該。 – mike00 2012-01-30 20:03:52