Can I define a generic method that accepts only an argument only from a certain class hierarchy, the parent and child classes?
void DoSomething<T>(T input) {}
class A {}
class B : A {}
Here I want to use something like:
void DoSomething<A>(A input) {}
and I want this to work also for all cases within the class hierarchy:
A objA;
B objB;
DoSomething(objA);
DoSomething(objB);
You can do that by applying a generic constraint for base class
void DoSomething<T>(T input) where T : A
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.