Given these 4 classes:
class Test<T1> where T1 : new()
{
public static string getName<T2>() where T2 : T1
{
return "OK";
}
}
abstract class A
{
protected A() { }
public int a;
public int b;
}
class B : A
{
public B() { }
public int c;
public int d;
}
class C : B
{
public int d;
public static void Main()
{
//What is the correct statement
}
}
Which of the following calls will return "OK"
According to the Test class, T1 must be non abstract and contain a public constructor with no parameters. The "where T1 : new()" constraint ensures this.
In the declaration of getName, T2 is given the constraint that it must inherit T1 "where T2 : T1".
A is abstract so it's can't be T1.
C can't be T1 because there must be a class that inherits T1 to use T2.
The only combination that works is Console.WriteLine(Test<B>.getName<C>());