C # inverter,

Source: Internet
Author: User

C # inverter,

In many. net books have seen the concept of inverter and collaborative change, and I also searched for some explanations about these two concepts on the Internet, but I have been feeling like I understand them, until recently I encountered a problem in the project, the inverter was used, and the understanding of the inverter finally took another step.

The inverter can only use generic interfaces and delegation. I have never understood why it should be used in generics. Today I finally want to understand it. Before introducing the inverter, let's talk about generics. generics are used to reuse algorithms. For example

 1 public class EntityBase<T> 2     { 3        DbContext db = new DbContext(); 4        public void Add(T Entity) 5        { 6            db.DbSet<T>.Add(Entity); 7        } 8        public void Remove(T Entity) 9        {10            db.DbSet<T>.Remove(Entity);11        }12     }

Familiar with it, right? This is the code we use in EF. Every entity class (the class mapped to the table in the data) is defined using this generic class, you only need to specify the T type. If this generic class is not used, we have to define these methods for each object class, the Code of methods in all classes is identical except for the type. So generics are the reuse of algorithms. generics are only visible to the compiler ,. net runtime environment does not know the generics, because during compilation, the compiler automatically generates method code for each type according to the T type.

The inverter of generic interfaces is actually used to reuse algorithms. The following example shows

1 public interface ICry <out T> 2 {3 void Cry (); 4} 5 public class Animal 6 {7 8} 9 public class Cat: Animal, ICry <Cat> 10 {11 public void Cry () 12 {13 Console. writeLine (""); 14} 15} 16 public class Tiger: Animal, ICry <Tiger> 17 {18 public void Cry () 19 {20 Console. writeLine ("Publish successfully succeeded"); 21} 22} 23 public class Nibian24 {25 public void Metho1 (ICry <Animal> an) 26 {27. cry (); 28} 29}

Note that the Nibian method is named ICry <Animal>, but ICry <Cat> or ICry <Tiger> can be passed, this is because we have written the out keyword in the generic interface. Although ICry <Cat> and ICry <Tiger> are not in the same class, in addition, it is not a parent-child relationship with ICry <Animal>. According to the OO principle, if the parameter is a parent class, only the parent class and sub-class instances can be passed. If this feature is not inverter, we must also write such a method for each subclass, but the parameters are ICry <Cat> and ICry <Tiger>. Haha, we can see that the inverter is used to achieve algorithm reuse. The Main method of the Main program

1 static void Main(string[] args)2         {3             ICry<Cat> c = new Cat();4             ICry<Tiger> t = new Tiger();5             Nibian nb = new Nibian();6             nb.Metho1(c);7             nb.Metho1(t);8         }

 

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.