0

我有一个看起来像这样的结构:

public struct Pair<T,U> {
    public readonly T Fst;
    public readonly U Snd;

    public Pair(T fst, U snd) {
        this.Fst = fst;
        this.Snd = snd;
    }

    public override String ToString() {
        return "(" + Fst +", " + Snd + ")";
    }
}

现在我需要声明一个类型的变量“约会” Pair<Pair<int,int>, String>

  1. 我该如何初始化它?
  2. 我如何访问约会.Fst.Snd?(它的类型应该是int)
4

2 回答 2

2

我不确定问题出在哪里。这不行吗?

Pair<Pair<int, int>, string> s = new Pair<Pair<int, int>, string>(new Pair<int, int>(5, 10), "hello");
 Console.WriteLine(s.Fst.Snd);
于 2012-06-03T10:13:42.377 回答
0

初始化可以这样完成

Pair<Pair<int, int>, String> appt = new Pair<Pair<int, int>, string>(new Pair<int,int>(1,3),"test");

然后您可以访问:

appt.Fst; // type pair<int,int>
appt.Snd; // type string
appt.Fst.Snd; // type int
于 2012-06-03T10:10:40.660 回答