简体   繁体   中英

Template struct inside a template struct

I have a struct that looks like this:

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 + ")";
    }
}

Now I need to declare the variable "appointment" of a type Pair<Pair<int,int>, String> .

  1. how do I initialize it?
  2. how do I access appointment.Fst.Snd? (Its type should be int)

I'm not exactly sure to understand where the problem is. Doesn't this work?

Pair<Pair<int, int>, string> s = new Pair<Pair<int, int>, string>(new Pair<int, int>(5, 10), "hello");
 Console.WriteLine(s.Fst.Snd);

Initialisation can be done like this

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

Then you can access:

appt.Fst; // type pair<int,int>
appt.Snd; // type string
appt.Fst.Snd; // type int

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM