When enforcing a Generic constraint
class GenericTest
{
public void Sample<T>(T someValue) where T:Racer,new()
{
Console.WriteLine(someValue.Car);
}
}
The Type T should be derived from the Base Type Racer (Correct me if anything wrong).
public class Racer
{
string name;
string car;
public Racer() { }
public Racer(string name, string car)
{
this.name = name;
this.car = car;
}
public string Name
{
get { return name; }
}
public string Car
{
get { return car; }
}
}
In "Main()" I am executing as
static void Main(string[] args)
{
List<Racer> rcr = new List<Racer>();
rcr.Add(new Racer("James","Ferrari"));
rcr.Add(new Racer("Azar","Bmw"));
rcr.Sort(delegate(Racer a, Racer b)
{return a.Name.CompareTo(b.Name); });
GenericTest tst = new GenericTest();
tst.Sample<Racer>(rcr[0]);
Console.ReadLine();
}
My Question is:
The constraint I implemented is where T:Racer,new()
,So T should be derived from Racer.
But In Main() I am passing ( tst.Sample<Racer>(rcr[0]);
) the type "Racer"
.The code is working.
How come the Racer be derived from Racer?