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

Example Analysis of Covariant and Inverter in C #

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

Share

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

This article mainly introduces the example analysis of covariant and inverter in C#, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

One: what is covariant and inverted

Covariant refers to a type that can use a greater (more specific) degree of derivation than the original specified derived type, and an inverter refers to a type that can use a less derived (less specific) degree than the originally specified derived type.

Only generic interfaces and generic delegate parameters support covariance and inversion

Introduction using System;using System.Collections.Generic; class MainClass {static void Main () {object o = "str"; List oList = new List (); IEnumerable strs = new List ();}}

In the above code, the first sentence is fine, it belongs to type-safe conversion, and the second sentence will report an error, because there is no inheritance relationship between the two list, and the third sentence is correct. In fact, behind it, it is covariance and inversion that are at work.

Three: covariation

Covariance is expressed as out in the parameters of generic methods. Using out, you can use subclass generic parameters to construct when declaring parent class generic parameters. Out parameters can be simply understood as output, as return values such as IEnumerable interface.

Using System;using System.Collections.Generic; class MainClass {static void Main () {IEnumerable list = new List ();}}

Analyze why the above code is legal? First, although it is declared in IEnumerable, it is constructed in List, and the elements in the list are of type string. Secondly, the function of IEnumerable is only traversing elements, and adding operations is not allowed, so it is legal, in essence, it is the principle of Richter substitution.

Four: inverter

Inverters are represented by in in the parameters of generic methods. Using in, you can use parent generic parameters when declaring subclass generic parameters. The int parameter can only be used as an incoming value, not as a return value, such as Action delegate.

Using System; class MainClass {static void Main () {Action action = new Action ((o) = > {}); action (");}}

Analyze why the above code is legal? It seems that object is converted to string, but in fact, when you use a delegate, you pass in a parameter of type string, and then convert string to object, which is essentially a conversion from a derived class to a base class, so it is type safe and essentially a Richter substitution principle.

Fifth: why are covariates and inverters for generic interfaces or generic delegate parameters?

Not for generic classes?

From the above, we can see that both covariant and inverter define method members (interfaces cannot define fields only define members), while method members do not involve object memory allocation when creating objects, so they are type safe, while generic classes are template classes. the class can contain fields, so it is not safe.

Using System;using System.Collections.Generic; class MainClass {static void Main () {object o1 = ""; / / Type-safe string S1 = (string) o1apacter / non-type-safe IEnumerable O2 = new List (); / / Covariant Action S2 = new Action ((o) = > {}); / / Inverter} VI: custom covariant using System;using System.Collections.Generic Class MainClass {static void Main () {ICustomCovariant o = new CustomCovariant ();}} public interface ICustomCovariant {T Get ();} public class CustomCovariant

< T>

: ICustomCovariant {public T Get () {return default (T);}} VII: custom inverter using System;using System.Collections.Generic; class MainClass {static void Main () {IContravariant o = new CustomContravariant ();}} public interface IContravariant {void Get (T t) } public class CustomContravariant: IContravariant {public void Get (T t) {}} Thank you for reading this article carefully. I hope the article "sample Analysis of Covariance and Inverter in C#" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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