I am trying to understand how restrictions (on a class or general methods) can affect the methods themselves. Take this code, for example:
class Base<T> where T:class, IComparable<T>
{
public virtual void Method(T obj) { }
}
class Derived<T> : Base<T> where T:class, IComparable<T>, IEnumerable<T>
{
public override void Method(T obj) { }
}
This code compiles fine, and the compiler / runtime is able to resolve polymorphic calls into the non-generic Method method, which takes a parameter that has a common type. In the base class and the derived class, the restrictions for the type parameter are different.
I also pointed out a class constraint to preserve value types, which could probably cause a problem, because a different class is created for each instance of the value type, while only one such class is created for reference types.
The following code, on the other hand, does not compile.
class Base
{
public virtual void Method<T>() where T : class, IComparable<T> { }
}
class Derived : Base
{
public override void Method<T>() where T : class, IComparable<T>, IEnumerable<T> { }
}
# , , . Google , - , ( ..). , 1 , , , . / 1, 2 ?