An example of covariance and inversion of new c#4.0 properties

Source: Internet
Author: User

In this paper, the covariance and inversion of c#4.0 new features are described, which will help us to master the c#4.0 program design. The specific analysis is as follows:


First, the c#3.0 and the inverse of the previous transformation

If this is the first time you've heard of the two words, don't worry, they're very common. The covariance and inversion (covariance and contravariance) in c#4.0 are further perfected, which are mainly two kinds of runtime (implicit) generic type parameter conversions. In simple terms, the so-called covariant (covariance) refers to the type from "small" to "large", such as from sub-class to the parent class, the inverse is the "big" to "small", the two have different conditions and uses. The following example illustrates C#3.0 's previous support for covariance and contravariance:

Code 1

public class Animal {}public class Cat:animal {}public delegate Animal Anihandler (Animal a);p ublic static Animal anime Thod (Animal a) {return null;} public static Cat Catmethod (Object o) {return null;} public static void Testcovariance () {  Anihandler handler1 = Animethod;  Anihandler Handler2 = catmethod;//here is legal}

The catmethod here is not strictly satisfied with the signature of the delegate Anihandler, but it is used as a anihandler is legal, under the action of covariant (Cat->animal) and Contravariance (Object->animal), the method that the delegate points to , the passed parameter can be a large, broad type, and the returned result can be a smaller, more precise type (subclass) because it contains more information. Note that this is said in the method, and in the perspective of the caller using the method, exactly the opposite, when invoking the method, the parameter can be a "small" subclass, and the return value can be used as a "large" parent class, such as the following call is legal:

Object o = Animethod (new Cat ());

Oh, it sounds a little dizzy, now I want to try to express the problem succinctly. Whether it's covariant or contravariant, it's all about making a very reasonable fact: if the type information provided is more than the type information needed (rather than equal), then of course it is possible. In the example of code 1, the Anihandler delegate needs a animal as the return value, but I return it to a cat,cat that contains all the features of animal, which is certainly possible, and that is covariance, and anihandler needs a animal as a parameter, In order for the function to get more information than required, I can only ask for an object to be passed in, which is certainly possible, which is the inverse.


Second, the covariance in c#4.0

Let's start with a look at how the harmonious covariance is happening. The covariance in c#4.0 is very similar to the loose delegate in c#3.0, and the new C # covariant feature is also reflected on the type parameters of the generic interface or generic delegate. In the case of classic animal and cat, after you have seen the code 1 above, since Cat Catmethod () can be used as animal anihandler, then you have every reason to believe that the following code is also valid in c#3.0:

Code 3

Delegate T thandler<t> (); static void Main (string[] args) {  thandler<cat> cathandler= () = new Cat (); C1/>thandler<animal> Anihandler = cathandler;//covariance}

Unfortunately, you are wrong, in c#3.0, the above code can not be compiled, you will be told that an error occurred!

Times have progressed, and now the compiler in c#4.0 is supporting the above notation. You only need to add an out keyword before declaring the type parameter of Thandler:

Delegate T Thandler<out t> ();

Using a single keyword instead of directly allowing implicit conversions is also for type safety reasons. So when you write out, you should know the covariance that might happen.


Three, the inversion in the c#4

We continue to use animal and cat for example, in VS2008, the following code cannot be compiled:

Code 5

delegate void Thandler<t> (T t);p ublic static void Testcontravariance () {  thandler<animal> Anihandler = ( ANI) = {};  thandler<cat> cathandler = Anihandler;}

And in VS2010, uh, not too. Oh, actually a little bit, here if the type parameter T is preceded by the keyword "in", that is, delegate void Thandler<in t> (T t), Cat->animal contravariance can be achieved.


Iv. Summary:

Covariance and contravariance in c#4 make it more natural to type conversions in generic programming, but it is important to note that the covariance and contravariance mentioned above are used only between reference types and are currently available only for generic interfaces and delegates. A T parameter can only be in or out, and you can't do that if you want your delegate parameter contravariant and return value covariance (as shown in code 1).

It is believed that this article has some reference function for us to master c#4.0 program design better.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
An example of covariance and inversion of new c#4.0 properties

This address: http://www.paobuke.com/develop/c-develop/pbk23536.html






Related Content C # Boxing and unboxing case Analysis C # recursive algorithm looking for the number of k large in the array ASP. 7 ways to get the current path in C # The C # implementation calculates a point around another point to rotate the specified arc recoil value method
C # memcache using the Introduction C # Implementation of C # text wrapping based on reflection and feature implementation of ORM Mapping example C # implement method of removing strings hollow lattice

An example of covariance and inversion of new c#4.0 properties

Related Article

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.