Create relationship between two type parameters : Generic Class « Generics « C# / C Sharp






Create relationship between two type parameters

 

using System;

class A {
}

class B : A {
}

// Here, V must inherit T.
class Gen<T, V> where V : T {
}

class Test {
  public static void Main() {

    // This declaration is OK because B inherits A.
    Gen<A, B> x = new Gen<A, B>();

    // This declaration is in error because
    // A does not inherit B.
//    Gen<B, A> y = new Gen<B, A>();

  }
}
           
         
  








Related examples in the same category

1.A simple generic classA simple generic class
2.Declare the generic class.
3.A Generic Class with Two Type ParametersA Generic Class with Two Type Parameters
4.Generic Fields
5.Demonstrate the default keywordDemonstrate the default keyword
6.Comparing Instances of a Type ParameterComparing Instances of a Type Parameter
7.'This' Reference for Generic Types
8.Generic class with interfaceGeneric class with interface