interfaces and abstract classes:
To better understand the interface, the most common problem is the difference between the interface and the abstract class. There are real differences between interfaces and abstract classes, which are embodied in:
1. A class can implement any number of interfaces, but only a subclass of an abstract class;
2. An abstract class can include non-abstract methods, and all the methods of an interface are abstract in effect;
3. An abstract class can declare and use variables, and an interface cannot;
4. The access modifier for a method in an abstract class can be: public,internal,protected,protected internal, or private, while the interface is public by default, and when declaring an interface member, Access modifiers are not allowed;
5. An abstract class can define constructors, and an interface cannot;
The above 5 point difference is mainly used, the reason for its division, is because its use of the scene is different, that is, the function of the main can be summarized as follows: Abstract class is used in the inheritance relationship with the family layer relationship, and the interface is used in a functional sense.
an implementation of an attribute in an interface:
Usually the interface is the corresponding method, but it can do the same thing with the property, first look at the following code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceconsoleapplication5{ Public InterfaceCeshi {intDA {Get; } Read-onlystringWenben {Get;Set; } } Public classJc:ceshi {Private intda; Private stringWenben; Public intDA {Get{returnda;} } Public stringWenben {Get{returnWenben;} Set{Wenben =value;} } } classProgram {Static voidMain (string[] args) {JC JC=NewJc (); Jc.wenben="Test"; Console.WriteLine ("Da:{0},wenben:{1}", Jc.da, Jc.wenben); Console.ReadLine (); } }}
Delegate Overview:
In many types of pointers in C, there is a function pointer, which refers to the entry address of the function. The concept of delegation is similar in C #. Unlike function pointers in the C language, delegates in. NET are type-safe.
Use of Delegates:
Similar to using a class, you first define the delegate you want to use, which defines what type of method the delegate represents, and then creates one or more instances of the delegate. Here is a sample code:
namespaceconsoleapplication5{classProgram {//The definition of a delegate, the type of the corresponding method Public Delegate intDelegateshowint (inti); Public Static intShowData (inti) {returni +1; } Static voidMain (string[] args) {Delegateshowint Showint=NewDelegateshowint (ShowData); Console.WriteLine ("result: {0}", Showint (1)); Console.ReadLine (); } }}
The above example can be seen in the general case of the application of a delegate, that is, in the case of a delegate using the corresponding method.
Attention:
for the formula: Delegateshowint showint = new Delegateshowint (ShowData) can be thought of as the "constructor" form of all delegates--whose arguments are function references.
Multicast delegation:
The previous example is the simplest and most common form (a delegate instance that calls a method). Because we can instantiate one or more instances of a delegate, on the other hand, we can have a delegate call multiple methods , which is called a multicast delegate.
namespaceconsoleapplication5{classProgram {//define the delegate, note that the return type is: void Public Delegate voidDelegateshowint (inti); Public Static voidShowData (inti) {Console.WriteLine ("Output result: {0}", i); } Public Static voidSHOWDATA2 (inti) {Console.WriteLine ("Output result: {0}", i +1); } Static voidMain (string[] args) { //How to use multicast delegates: + =//delegates can use the "+" operator to group multiple delegates together, or subtract a delegate from a combined delegate using the "-" Operation symbolDelegateshowint Showint =NewDelegateshowint (ShowData);//Instance Delegate instanceShowint + =NewDelegateshowint (SHOWDATA2); Showint (1);//you can call the ShowData method directly using a delegateShowint (2); Console.ReadLine (); } }}
Through the example, we can see that we have implemented multiple methods of binding to the delegate object.
Note: The return type of the multicast delegate is required to be void, because if there is a return value, there will be no meaning, only the last function that has been called!
Event Overview:
With the foregoing on the delegation of the narrative, for here to explain the events to pave the groundwork. event, which is greatly applied in Windows Form, here is an example of the most common click button that triggers an event.
1 This. button. Location =NewSystem.Drawing.Point (458, the);2 This. button. Name =the button";3 This. button. Size =NewSystem.Drawing.Size ( the, at);4 This. button. TabIndex =2;5 This. button. Text ="measured";6 This. button. Usevisualstylebackcolor =true;7 This. button. Click + =NewSystem.EventHandler ( This. Button_Click);
This is a button that is added to Windows Form, and on line 7th, the event click on the object button is given a delegate that specifies the form:
Private void Button_Click (object sender, EventArgs e) { }
The parameters of the corresponding type contain object and EventArgs, and return as void form. Such an event is associated with a delegate.
All in all, it is visible:
1. A delegate, as a special type, is similar to defining a class (in fact, it is treated as a class in the background);
2. Event, which regulates its type (delegate type);
In conclusion, the interface cannot contain delegates, and why can an interface contain events?
Because a delegate is a special type of class equivalent (the instance of a class is the managed memory space where the data is stored, and the delegate has only the method address), the interface cannot contain the delegate; The event is not the same level as the class or interface, and the event's unique mechanism makes the event a standard component of an interface. As long as the inherited class contains a public event member.
Custom events:
namespaceconsoleapplication5{//define a delegate type first Public Delegate voidDelegatedog (); Public classDog {//defines the event type, which corresponds to the custom delegate type Public EventDelegatedog Eventdog; Private stringname; Public stringName {Get{returnname;} Set{name =value;} } Public voidinvoked () {//using event methods: As with their corresponding methodsEventdog (); } classProgram { Public Static voidResponse () {Console.WriteLine ("Output Content"); } Static voidMain (string[] args) {Dog Dog=NewDog (); Dog. Name="AA"; //The operation of the event is related to the corresponding delegate, and the delegate invokes the response methodDog. Eventdog + =NewDelegatedog (Response); //Triggering EventsDog. Invoked (); Console.ReadLine (); } } }}
From the example above, you can see how custom events are defined, assigned, used, etc., or you can deepen your understanding of events.
interfaces, delegates, events