* from C# 4.0 start , Both generic interfaces and generic delegates support covariance and inversion , For historical reasons , Arrays also support covariance .
* Richter's principle of substitution : Where any base class can appear , Subclass must appear .
covariant (out)
* covariant : That is, natural change , Follow the principle of Richter's replacement , In code, any base class can be assigned by its subclass , as Animal = Dog,Animal = Cat
* use out Keyword declaration ( Attention and method out Different meanings )
* The marked parameter type can only be used as the return value of a method ( Include read-only properties )
* In the absence of covariance :abstract class Animal {} class Dog : Animal {} class Cat : Animal {}
interface IPoppable<T> { T Pop(); } class MyStack<T> : IPoppable<T> { private
int _pos; private readonly T[] _data = new T[100]; public void Push(T obj) =>
_data[_pos++] = obj; public T Pop() => _data[--_pos]; } The following code cannot be compiled var dogs =
new MyStack<Dog>(); IPoppable<Animal> animals1 = dogs; // A compilation error will occur here
Stack<Animal> animals2 = dogs; // A compilation error will occur here here , If we need to add a new input parameter for the zoo keeper
Stack<Animal> Feeding method , A better way is to add a constraint generic method :class Zookeeper { public static void
Feed<T>(IPoppable<T> animals) where T : Animal {} } // perhaps class Zookeeper {
public static void Feed<T>(Stack<T> animals) where T : Animal {} } // Main
Zookeeper.Feed(dogs);
* Now? ,C# Increased covariance
send IPoppable<T> Interface support covariance // Just one more out statement interface IPoppable<out T> { T Pop(); }
simplify Feed method class Zookeeper { public static void Feed(IPoppable<Animal> animals) {}
} // Main Zookeeper.Feed(dogs);
The nature of covariance —— Can only be returned as a method , Interface ( Or commission ) Cannot add element outside , Ensure generic type security , So don't worry Dog Appears in the collection of Cat
* Common interfaces and delegates that support covariance are :
* IEnumerable
* IEnumerator
* IQueryable
* IGrouping<out TKey, out TElement>
* Func Isochronous 17 individual
* Converter<in TInput, out TOutput> IEnumerable<Dog> dogs =
Enumerable.Empty<Dog>(); IEnumerable<Animal> animals = dogs; var dogList = new
List<Dog>(); IEnumerable<Animal> animals = dogList;
* in addition , For historical reasons , Arrays also support covariance , for example var dogs = new Dog[10]; Animal[] animals = dogs;
But type security cannot be guaranteed , The following code can be compiled normally , But the runtime will report an error animals[0] = new Cat(); // Error will be reported at runtime
Inverter (in)
* Inverter : That is, the converse change of covariance , In essence, we should follow the principle of Richter's replacement , Assign subclass to base class
* use in Keyword declaration
* Marked parameter types can only be used as method input parameters ( Include write only properties )
* for example :abstract class Animal {} class Dog : Animal {} class Cat : Animal {}
interface IPushable<in T> { void Push(T obj); } class MyStack<T> : IPushable<T>
{ private int _pos; private readonly T[] _data = new T[100]; public void Push(T
obj) => _data[_pos++] = obj; public T Pop() => _data[--_pos]; } // Main var
animals = new MyStack<Animal>(); animals.Push(new Cat()); IPushable<Dog> dogs =
animals; dogs.Push(new Dog());
Natural characteristics of inverter —— Only as method input parameter , Interface ( Or commission ) Cannot get element , That is, you can only assign a subclass to a parent class , Thus, type safety is ensured .
* in addition , Commonly used interfaces and delegates that support inversion are :
* IComparer
* IComparable
* IEqualityComparer
* Action Isochronous 16 individual
* Predicate
* Comparison
* Converter<in TInput, out TOutput> Action<Animal> animalAction = new
Action<Animal>(a => { }); Action<Dog> DogAction = animalAction;
Technology
Daily Recommendation