In a different StackExchange question, I noticed someone using this prototype:
void DoSomething<T>(T arg) where T: SomeSpecificReferenceType
{
//Code....
}
Bearing in mind there is only a single type constraint (SomeSpecificReferenceType
), what is the difference and advantage of writing it like that, instead of simply:
void DoSomething(SomeSpecificReferenceType arg)
{
//Code....
}
In both cases, arg
will be subject to compile-time type checking. In both cases, the body of the method can safely rely on knowledge that arg
is of (or is a descendant of) a specific type that is known at compile time.
Is this a case of an overzealous developer learning about generics before learning about ordinary inheritance? Or is there a legitimate reason why a method signature would be written this way?