C # study notes-Delegate commission

xiaoxiao2021-03-06  75

DELEGATE: Simple can be understood as verb noun. The delegation is a reference to a function, the return value type of the ownership and function, and the delegate is actually a class, he can hold a reference to a method, the class has a signature, which can only be applied with its signature . It is because a delegate is an instantiated class (an object), so the delegate is controlled and secure. Implement a delegation: 1: Declaring the Delegate object, with the same return value type 2: Create a Delegate object, use the function you want to reference as a parameter 3: In the place where asynchronous calls, pass Create a delegate object (an instance of a delegate class) to call the method application When the Win32 function needs to return multiple data, it is usually implemented by a callback mechanism. Developers pass the function pointer to the function and then call the developer's function for each item. There is no function pointer in the C #, but use "delegate" to use the delegate instead of the function pointer when calling the Win32 function. Delegation call: Method 1: After creating a delegate object, pass the entrusted object to other code (such as a method). Call the entrusted object EG: // declaration by the name of the entrusted object (back with the parameters to be transmitted to the entrusted): public class delegateclass {public delegate void delegatea (String s); Private Void Fa (String s) {MessageBox.show ("hi" s);} private void fb (string s) {messagebox.show ("88" s);} private void fc (string s) {messagebox.show ("this is" s);} Public void Dodelegatea (DELEGATEA DA, STRING INFO) {DA (Info);}} Call: Instantiation Class: DelegateClass DC = New DelegateClass (); DC.DodeLegatea (New Delegatea (FA), "Haha"); // FA is passed to a delegatea entrusted instance dc.dodelegatea (New delegatea (fb), "hoho"); the output message box is displayed: Hi Haha88 HOHO Method 2: Instantiate the entrusted object, direct delivery parameters to delegate object calls or For example, delegatea delegatea = new delegatea (fc); delegatea ("heihei"); DELEGATEA

Supplement ...

转载请注明原文地址:https://www.9cbs.com/read-97870.html

New Post(0)