Search code examples
c#genericsnestednested-generics

Create value nested generic type and assigne it to variable


here I created the variable appointment of type Pair<Pair <int, int>, String> I would like to create a value of this type and assign it to appointment.

Could somebody help me? I am new to C# and programming.

Pair< Pair<int, int>, String> appointment = new Pair<Pair<int,int>,String>; 

Solution

  • Assuming that Pair has a constructor with two arguments

    Pair<Pair<int,int>,string> appointment = new Pair<Pair<int,int>,string>(
        new Pair<int,int>(x, y),
        "hello"
    );
    

    or more concise

    var appointment = new Pair<Pair<int,int>,string>(
        new Pair<int,int>(x, y),
        "hello"
    );
    

    or with object initializers (assuming Pair has two properties named Arg1 and Arg2)

    var appointment = new Pair<Pair<int,int>,string> {
        Arg1 = new Pair<int,int> { Arg1 = x, Arg2 = y),
        Arg2 = "hello"
    };
    

    or if you want to create an empty Pair

    var appointment = new Pair<Pair<int,int>,string>();