SoFunction
Updated on 2025-03-08

C# Covariance and Inverter Example Analysis

This article describes the principles and applications of C# covariance and inversion. Share it for your reference. The details are as follows:

The transition from the subclass to the parent class is covariance, which is marked with the out keyword, and the transition from the parent class to the child class is inverse, and the in keyword is in

Applications of covariance and inversion
 
1. Covariance of arrays
 

Copy the codeThe code is as follows:
Animal[] animalArray = new Dog[]{};

Note: The declared array data type is Animal, but in fact, the Dog array is given when assigning; each Dog object can be safely converted to Animal. Dog's transformation to Animal method is a transformation upward along the inheritance chain, so it is covariation.
 
two. Covariance and inversion in commission

1. Covariance in commission

Copy the codeThe code is as follows:
//The return value defined by the delegate is Animal type is the parent class
public delegate Animal GetAnimal();
//The return value in the delegate method implementation is Dog, which is a subclass
static Dog GetDog(){return new Dog();}
//The return value of GetDog is Dog, Dog is a subclass of Animal; returning a Dog is definitely equivalent to returning an Animal; so the following assignment for the delegate is valid
GetAnimal getMethod = GetDog;

 
2. Inversion in delegation

Copy the codeThe code is as follows:
//The parameter type defined in the delegate is Dog
public delegate void FeedDog(Dog target);
//The parameter type in the actual method is Animal
static void FeedAnimal(Animal target){}
// FeedAnimal is an effective method for FeedDog delegation, because the parameter type accepted by the delegate is Dog; while the parameter accepted by FeedAnimal is animal, Dog can be implicitly converted into Animal, so the delegate can safely perform type conversion and execute the delegate method correctly;
FeedDog feedDogMethod = FeedAnimal;
//The parameters when defining the delegate are subclasses. In fact, the parameters of the delegate method are the broader parent class Animal, which is the parent class changing toward the child class, which is an inversion

 
III. Covariance and inversion of generic delegations
 
1. Inversion in generic delegation

Copy the codeThe code is as follows:
//Appointment Statement:
public delegate void Feed<in T>(T target);
//Feed delegate accepts a generic type T. Note that there is an in keyword in the angle brackets of the generic. The function of this keyword is to tell the compiler that type T may be inverted when assigning a delegate.

//Declare a delegation of T as Animal
Feed<Animal> feedAnimalMethod = a=>(“Feed animal lambda”);
// Assigning the delegate with T as Animal to the delegate variable with T as Dog is legal, because there is an in keyword when defining a generic delegate. If the in keyword is removed, the compiler will consider it illegal
Feed<Dog> feedDogMethod = feedAnimalMethod;


 
2. Covariance in generic delegation

Copy the codeThe code is as follows:
//Appointment Statement
public delegate T Find<out T>();
//Find delegate returns an instance of generic type T. There is an out keyword in the angle brackets of the generic type, which indicates that the T type may be covaried.
//Declare the Find<Dog> delegation
Find<Dog> findDog = ()=>new Dog();
//It is legal to declare Find<Animal> delegate and assign findDog to findAnimal. The transformation of type T from Dog to Animal is covariant
Find<Animal> findAnimal = findDog;

 
Four. Covariance and inversion in generic interfaces
 
1. Inversion in generic interfaces

Copy the codeThe code is as follows:
//Interface definition:
public interface IFeedable<in T>
{
void Feed(T t);
}

//The generic T of the interface has an in keyword before, indicating that this generic interface may need to be inverted
//The following generic type FeedImp<T> implements the above generic interface; it should be noted that the covariance and inverter keywords are in

public class FeedImp<T>:IFeedable<T>
{
    public void Feed(T t){
        (“Feed Animal”);
    }
}

//Use interface inverter:
IFeedable<Dog> feedDog = new FeedImp<Animal>();
//The above code assigns the FeedImp<Animal> type to the variable of IFeedable<Dog>; Animal has changed to Dog, so it is an inversion


 
2. Covariance in generic interfaces

Copy the codeThe code is as follows:
//Definition of interface:
public interface IFinder<out T>
{
    T Find();
}

//The generic T of the generic interface used the out keyword to indicate that this interface may need to be covaried; the following generic interface implementation class
public class Finder<T>:IFinder<T> where T:new()
{
    public T Find(){
        return new T();
    }

//Using covariance, the generic type of IFinder is Animal, but because of the out keyword, I can assign Finder<Dog> to it
IFinder<Animal> finder = new Finder<Dog>();

I hope this article will be helpful to everyone's C# programming.