In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to realize the covariant and inverter of the C# generic interface". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to realize the covariant and inverter of the C# generic interface" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.
1. What is covariant and inverted?
Suppose: TSub is a subclass of TParent.
Covariant: if a generic interface IFoo,IFoo can be converted to IFoo, we call this process covariant, and IFoo supports covariant for parameter T.
Inverter: if a generic interface IFoo,IFoo can be converted to IFoo, we call this process an inverter, and IFoo supports the inversion of the parameter T.
2. Why should there be covariation and inversion?
Usually only objects with inheritance relationships can have implicit type conversions, such as Base b=new sub ().
Covariant and inverter can enable more types to achieve implicit type conversion and type security can be guaranteed.
3. Why should covariant and inverter be introduced into generic interfaces?
For the above reasons, many interfaces only use type parameters for parameters or return values. Therefore, there is more flexibility in supporting the use of covariant and inverted generics.
4. Why can parameters that support covariant only be used for the return value of the method? Parameters that support inverter can only be used for method parameters?
"TParent cannot be safely converted to TSub" is the common cause of these two problems.
We define an interface IFoo.
Interface IFoo {void Method1 (T param); T Method2 ();}
Let's look at the covariant process: the conversion from IFoo to IFoo.
Method1: replacing TSub with TParent,Method1 obviously has a TParent-to-TSub conversion.
Method2: the return value type has been changed from TSub to TParent, which is type safe.
So parameters that support covariance can only be used in the return value of the method.
Take another look at the inversion process: the conversion from IFoo to IFoo.
Method1: replacing TParent with TSub,Method1 has a TSub-to-TParent conversion, which is type safe.
Method2: the return type has been changed from TParent to TSub, which is not safe.
So the parameters that support inversion can only be used in the parameters of the method.
5. How does the generic interface support covariant and inverter and not support covariant and inverter?
This is actually a supplement to three questions.
Define an interface IFoo that supports neither covariance nor inversion.
Interface IFoo {void Method1 (T param); T Method2 ();}
Implement interface IFoo
Public class FooClass: IFoo {public void Method1 (T param) {Console.WriteLine (default (T));} public T Method2 () {return default (T);}}
Define an interface IBar to support covariation of parameter T
Interface IBar {T Method ();}
Implement interface IBar
Public class BarClass: IBar {public T Method () {return default (T);}}
Define an interface IBaz to support the inverter of parameter T
Interface IBaz {void Method (T param);}
Implement interface IBaz
Public class BazClass: IBaz {public void Method (T param) {Console.WriteLine (param.ToString ());}}
Define two types with inheritance relationships, IParent and SubClass.
Interface IParent {void DoSomething ();} public class SubClass: IParent {public void DoSomething () {Console.WriteLine ("SubMethod");}}
According to the covariant logic, use IFoo and IBar respectively.
/ / IFoo does not support covariant IFoo foo_sub = new FooClass () for parameter T; IFoo foo_parent = foo_sub;// compilation error / / IBar supports covariant IBar bar_sub = new BarClass () for parameter T; IBar bar_parent = bar_sub
Foo_parent = foo_sub prompts compile-time error "unable to implicitly convert type" IFoo "to" IFoo ". There is an explicit conversion (is there a missing cast?)
According to the logic of inversion, use IFoo and IBaz respectively.
/ / IFoo is incompatible with parameter T inverter IFoo foo_parent = null; IFoo foo_sub = foo_parent;// compilation error / / IBaz is compatible with parameter T inverter IBaz baz_parent = null; IBaz baz_sub = baz_parent
Foo_sub = foo_parent prompts compile-time error "unable to implicitly convert type" IFoo "to" IFoo ". There is an explicit conversion (is there a missing cast?)
6. Net 4.0 modification of IEnumerable interface?
Definition in 2.0:
Public interface IEnumerable: IEnumerable {IEnumerator GetEnumerator ();}
The definition in 4.0:
Public interface IEnumerable: IEnumerable {IEnumerator GetEnumerator ();}
You can see that support for covariance has been added in 4.0.
You can try it in two versions, and the following statement will report an error under 2.0.
List subarr = new List (); IEnumerable parentarr = subarr; read here, this article "how to realize the covariant and inverter of C# generic interface" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.