Summary of the inverse and covariance of generics

Source: Internet
Author: User

Generalization of the inverse and covariance of generics C # generic Inverse covariant

    • The concept of change
    • Covariant (foo< parent class > = foo< subclass >)
    • Contravariance (foo< Subclass > = foo< parent >)
    • The interaction between contravariance and covariance

The concept of change
  1. //Parent class = Subclass
  2. string str = "string";
  3. object obj = str; //Changed
Covariant (foo< parent class > = foo< subclass >)
  1. //Generic delegate:
  2. public delegate T myfunca<t> (); //does not support contravariance and covariance  
  3. Public Delegate T myfuncb< out t> (); //Support co-change
  4. myfunca<Object> funcaobject = null;
  5. myfunca<string> funcastring = null;
  6. myfuncb<Object> funcbobject = null;
  7. myfuncb<string> funcbstring = null;
  8. Funcaobject = funcastring; //Compile failed, Myfunca does not support contravariance and covariance
  9. Funcbobject = funcbstring; //changed, covariant
Contravariance (foo< Subclass > = foo< parent >)
  1. public delegate void myactiona<t > (T param); //does not support contravariance and covariance  
  2. Public Delegate void myactionb< in t> (T param); //Support inverter
  3. myactiona<Object> actionaobject = null;
  4. myactiona<string> actionastring = null;
  5. myactionb<Object> actionbobject = null;
  6. myactionb<string> actionbstring = null;
  7. actionastring = Actionaobject; //myactiona does not support contravariance and covariance, compilation fails
  8. actionbstring = Actionbobject; //changed, invert

If the In keyword is added as a generic modifier on a generic parameter, then that generic parameter can only be used as the input parameter of the method, or as a parameter of a write-only property, not as a method return value, etc., in short, can only be "in", not out. The Out keyword instead.

The interaction between contravariance and covariance
  1. Public Interface IBar< in T> {}
  2. //should be in
  3. Public Interface IFoo< in T>
  4. {
  5. void Test(ibar<t> bar);
  6. }
  7. //or out
  8. Public Interface IFoo< out T>
  9. {
  10. void Test(ibar<t> bar);
  11. }

IFoo with covariant capability requires that its generic parameters (IBar) have the inverse capability

Summary of the inverse and covariance of generics

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.