C# supports variance in delegate types with matching method signatures. This feature was introduced in .NET Framework 3.5. This means delegates can now be assigned with matching signatures but also that methods can return derived types.
If a method has a return type derived from the one defined in a delegate, it is defined as covariance in delegates. Similarly, if a method has fewer derived parameter types than those defined in a delegate, it is defined as contravariance.
Let's look at an example to understand covariance. For the purpose of this example, we will create a few classes.
Here, we will create the ParentReturnClass, Child1ReturnClass, and Child2Return classes. Each of these has a string type property. Both child classes are inherited from ParentReturnClass:
internal class ParentReturnClass
{
public string Message { get; set; }
}
internal class...