Event, time
1. multicast Delegation
Multicast delegation is a list of methods: + = adding methods to the list tail;-= means matching starts from the tail,
Only remove the first method for exact matching. If no matching is found, no error is returned.
Class MultiDelegateShow {public void MultiDelegateExample () {// multicast delegate is a method list: + = Add a method to the list tail;-= indicates matching from the tail, // only remove the first method for exact matching. If no matching is found, no error is returned. OtherClass otherclass = new OtherClass (); Console. writeLine ("****************************** + = add method list *** *****************************"); action del1 = new Action (this. sameClassMethod); // Add 1. in the same way, del1 + = new OtherClass (). otherClassMethod; // Add 2. the instance methods of different classes are del1 + = OtherClass. staticMethod; // Add 3. static Method del1 + = () => Console. writeLine ("LambdaMethod is be calling"); // Add 4. lambda expression del1 + = otherclass. otherClassMethod; // Add 5. note the difference with 2: del1.Invoke (); // the program will call the method Console according to 1, 2, 3, 4. writeLine ("******************************-= Delete method list *** *****************************"); del1-= this. sameClassMethod; // start searching from the tail (,) and match to deleting del1-= OtherClass. staticMethod; // static method del1-= new OtherClass (). otherClassMethod; // It cannot be deleted here, because it is the method of del1-= () => Console for different instances. writeLine ("LambdaMethod is be calling"); // the Lambda Method is del1-= otherclass that cannot be removed. otherClassMethod; // This is the same instance method, removed del1.Invoke (); Console. writeLine ("************************** multicast delegation with return values **** **************************"); func <string> del2 = new Func <string> (otherclass. withReturnMethod); del2 + = () => "1"; del2 + = () => "2"; del2 + = () => "3 "; del2 + = () => "4"; Console. writeLine (del2.Invoke (); // multicast delegate with return values. Only the return value Console of the last method can be obtained. writeLine ("****************************** asynchronous call of multicast delegate ** ******************************"); // del1.BeginInvoke (null, null); the multicast delegate cannot directly and asynchronously call foreach (Action item in del1.GetInvocationList () // This is equivalent to opening a thread {item for each delegate. beginInvoke (null, null) ;}} private void SameClassMethod () {Console. writeLine ("SameClassMethod is be calling") ;}} class OtherClass {public void OtherClassMethod () {Console. writeLine ("OtherClassMethod is be calling");} public static void StaticMethod () {Console. writeLine ("StaticMethod is be calling");} public string WithReturnMethod () {return string. format ("This is a method with a string return ");}}
2. Significance of delegation: decoupling
Asynchronous Multithreading
Multicast Delegation
3. Events
An Event is a delegated instance, and an Event keyword is added.
Delegation is a type, and events are delegated instances.
After the Event keyword is added, the permission is controlled so that external calls or direct value assignment are not allowed.
Class Program {static void Main (string [] args) {Console. writeLine ("***************************** EventShow ******* *************************"); eventShow eventshow = new EventShow (); // Action catdelegate = Dog. run; delegate can be directly assigned a value // catdelegate. invoke (); Delegate can call // catdelegate = null directly; delegate can assign null // eventshow. catEventhandle = Dog. run; the event cannot be assigned a value directly. // eventshow. catEventhandle. invoke (); events do not allow external calls to directly control the permissions. events can only appear on the left of + =,-= eventshow. catEventhandle + = Dog. run; eventshow. catEventhandle + = Baby. cry; eventshow. catEventhandle + = Father. turn; eventshow. catEventhandle + = Mother. hug; eventshow. catMiao (); Console. readKey ();}
class EventShow { public event Action catEventhandle=null; public void CatMiao() { Console.WriteLine("cat Miao"); if(catEventhandle!=null) { catEventhandle.Invoke(); } } }
class Dog { public static void Run() { Console.WriteLine("Dog Run"); } }