Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize covariant and Inverter in C #

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to realize covariant and inverter in C#". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Preface

In C # programming, due to the existence of forced conversion between types, it is easy to appear the so-called type variability, there are three kinds of covariant, inverter and invariant.

Like the concept of generics introduced in the previous article, if you create an instance of a generic type, the compiler accepts the generic type declaration and type parameters to create the constructed type. However, in the course of daily use, we may assign derived types to variables of the base type, sometimes with errors.

There is a problem of assignment compatibility.

Each variable has a type that assigns an instance of a derived class object to a base class variable (just as a variable declared by a child class can be assigned to a variable declared by a parent class).

As follows:

Class People {public int Age = 27;} class AhuiPeople: People {} People ahui = new People (); People people = new AhuiPeople (); Console.WriteLine ("Age:" + people.Age); Console.ReadKey ()

Covariant and inverter

According to the same logic, when we do this strongly typed conversion in a generic delegate, we will find that even if the normal conversion can be done between the base class and the derived class, there will be an exception error if the conversion cannot be performed between the delegates.

As shown in the following code:

Delegate T AgeDelegate (); static AhuiPeople GetAge () {return new AhuiPeople ();}

The specific use of the delegate during the conversion process, but in this way the compiler prompts for an error.

AgeDelegate ahui = GetAge; AgeDelegate people = ahui

Error prompt

As explained above, conversions can be performed between base and derived classes, but there is no association between delegates, and forced type conversions cannot be performed.

Then if you want to solve this problem, covariance is introduced to solve it.

If the derived class is only used to output values, then the constant relationship between the structured delegate validity is called covariant, and the type parameters in the delegate declaration can be marked with the Out keyword by proactively telling the compiler what we expect.

Delegate T AgeDelegate ()

After being modified like this, the code compiler demonstrated above can be compiled and passed normally.

The covariant is briefly introduced above, so let's take a look at what the inverter is.

In fact, the inverter is to declare not only the delegate type in the delegate, but also the argument in the delegate method.

This feature that allows derived objects to be passed in when it is expected to pass in the base class is called an inversion. Inverters are marked with the keyword in.

As shown in the following code:

Delegate void AgeDelegate (T p); static void GetAge (People p) {Console.WriteLine (p.Age);} AgeDelegate ahui = GetAge; AgeDelegate people = ahui; people (new AhuiPeople ()); Console.WriteLine (); Console.ReadKey ()

Output result

Since covariates and inverters can be used on delegates, they can also be used on interfaces, where you also need to use the out and in keywords.

This is the end of the content of "how to realize covariant and inverter in C#". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report