Home » C# Delegate Covariance

C# Delegate Covariance

by Online Tutorials Library

C# Delegates Covariance

C# Delegate Covariance allows us to call a method that has derived return type of the delegate signature return type.

It means we can call a method that returns parent or child class object.

Here, we are creating two examples. First example calls a method that returns a parent or derived class object.


C# Delegate Covariance Example 1

Output:

This is MethodA This is MethodB 

C# Delegate Covariance Example 2

In this example, we are calling a method that does not return derived object as specified in delegate signature. Let’s see what happen, if we do this.

Output:

DelegateCoveriance.cs(25,31): error CS0407:  'CSharpFeatures.C CSharpFeatures.DelegateCoveriance.MethodC()' has the wrong return type 

You may also like