"Go" C # covariance and Contravariance

Source: Internet
Author: User

This article transferred from: http://www.cnblogs.com/rr163/p/4047404.html

Covariance and Contravariance in C #

The transition from a subclass to a parent class is covariant, identified by the Out keyword, and transformed from the parent class to the subclass in the inverse, with the In keyword

The application of covariant and inverse transformation

One, the covariance of the array

animal[] Animalarray = new dog[]{};

  

说明:声明的数组数据类型是Animal,而实际上赋值时给的是Dog数组;每一个Dog对象都可以安全的转变为Animal。Dog向Animal方法转变是沿着继承链向上转变的所以是协变 二. 委托中的协变和逆变 1、委托中的协变
//委托定义的返回值是Animal类型是父类
Public delegate Animal Getanimal ();
//委托方法实现中的返回值是Dog,是子类
Static Dog Getdog () {return new Dog ();}
//GetDog的返回值是Dog, Dog是Animal的子类;返回一个Dog肯定就相当于返回了一个Animal;所以下面对委托的赋值是有效的  
Getanimal GetMethod = Getdog;

2, the inverter in the delegation

The defined parameter type in the delegate is the dog
public delegate void Feeddog (Dog target);
The parameter type in the actual method is animal
static void Feedanimal (Animal target) {}
Feedanimal is a valid method of Feeddog delegate, because the type of argument accepted by the delegate is dog, and Feedanimal accepts the argument that Animal,dog can be implicitly converted to animal, so the delegate can do the type conversion safely, Proper execution of the Entrustment method;  

Three. Covariance and contravariance of generic delegates

1, the inverse of the generic delegate

Delegate declaration:
The feed delegate accepts a generic type T, noting that there is an in keyword in the angle brackets of the generic that the function of the keyword is to tell the compiler that the type T may have to be reversed when assigning a value to a delegate
First declare a delegate with T as Animal
feed<animal> Feedanimalmethod = A=>console.writeline ("Feed Animal Lambda");
It is legal to assign a delegate of T to animal to a delegate variable that is a dog, because the IN keyword is defined when you define a generic delegate, and if you remove the In keyword, the compiler considers it illegal

2. Covariance in generic delegates

Delegate declaration

The Find delegate returns an instance of a generic type T, with an out keyword in the angle brackets of the generic that indicates that the T type is likely to be covariant

Declaring a find<dog> delegate
Find<dog> Finddog = () =>new Dog ();
Declaring a find<animal> delegate and assigning Finddog to Findanimal is legal, and type T from dog to Animal is covariant

Four Covariance and contravariance in generic interfaces

1, the inverter in the generic interface

Interface Definition:
public interface Ifeedable<in t>
{void Feed (T t);} There is an in keyword before the generic T of the interface, to indicate that the generic interface may have to do the inverse//the following generic type Feedimp<t>, to implement the generic interface above, and to note that the covariant and contravariant keywords Inpublic class feedimp<t >:IFeedable<T>{public    void feed (T t){        Console.WriteLine ("Feed Animal");}    }// Using interface inversion:ifeedable<dog> Feeddog = new feedimp<animal> (); The above code assigns the feedimp<animal> type to the ifeedable<dog> variable; Animal changes to the Dog, so it's the inverse  .

Go to C # covariance and contravariance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.