Delegate and event in. Net Framework
Although the above example completes what we want to do, we are not only confused: Why is the event model in. Net Framework different from the above? Why are there many EventArgs parameters?
Before answering the above questions, we should first understand the encoding specifications of. Net Framework:
The name of the delegate type should end with EventHandler.
Prototy
I wrote an example today to illustrate the differences between delegate and delegate. The post is as follows, and the comments are complete:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
1
Using
System;
2
Using
System. Collections. Generic;
3
Using
System. text;
4
Using
System. Resources;
5
Using
System. reflecti
Following the previous EventHandler, continue to pits, and briefly understand the FuncMSDN's explanation for both:Functhe type value specified by the TResult parameter. Action: Encapsulates a method that does not have parameters and does not return a value.The difference between the two is that there is no return value.As for funcActionUse the example above on MSDN to illustrate how they are used:FuncusingSystem;usingSystem.IO; Public classtestdelegate{ Public Static voidMain () {outputtarget ou
Delegate (3) delegate in. net 2.0,. net2.0
Since. net 2.0 introduces the anonymous method, the method for creating delegation can be simplified.Delegate in. net 2.0
WithAnonymous MethodThe previous example can be simplified:
1 namespace DelegateDemo 2 {3 // declare 4 public delegate void MyDel (string arg1, string arg2); 5 6 class Program 7 {8 static void Main
Action
Encapsulate a method delegate that contains two parameters without returning values.Syntax
public delegate void Action
Type parameter
In T1: the first parameter type of the delegate encapsulation method. This parameter type is inverter.Usage
You can use the Action
The following explicitly declares a delegate n
Here is an example of the relationship between Uitableviewcontroller and UITableView://------------------------------------------------------------------------//UITableViewController.h@interfaceUitableviewcontroller:uiviewcontroller @property (nonatomic,retain) UITableView*TableView;@end//------------------------------------------------------------------------//UITableView.h@protocolUitableviewdatasource;@protocolUitableviewdelegate@optional//The delegate
Delegate event proxy, delegate event
If you have used jquery, I believe you are very impressed with the delegate method. Its predecessor is the live method. You can bind a listening event before the node appears. This may not be clear.
First, let's take a look at the following example:
Now we need to bind a click event to each li in ul. the most stupid method
IOS development-pass the value through the Protocol and the Delegate agent (Delegate)Because Object-C does not support multi-inheritance, it is often replaced by Protocol. Protocol can define only one set of public interfaces, but it cannot provide specific implementation methods. That is to say, it only tells you what to do, but does not care about how to do it. When a class uses a Protocol (Protocol), it
1.Actionis divided into a generic and non-generic, with generics that can pass in any type of parameter.The format is as follows:1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.Windows.Input;7 8 namespaceDemo19 {Ten class Program One { A Static voidMain (string[] args) - { - //Generic Delegate the //Action: - -
Sort out the delegate concepts and the delegate concepts
I. Preface
The project size may be small. In addition to adding, deleting, modifying, and querying the project, only the business processes are left. What has not been figured out before is blurred after a long time...
II. Application scenarios
MSDN: delegate is a reference type that can be used to enca
On the Commission and events, the introduction of the Internet is very much, no longer to repeat, I recently need to tinker with some of the commissioned, so the Commission to do a certain amount of work, so with reflection to the members of the delegation, and look at the following code:
delegate void Delegatedemo ();
Static event Delegatedemo Delhandle;
static void Main ()
{
Delhandle + = new Delegatedemo (mydel
Existing two Views (Viewcontroller,viewcontroller1), jump to ViewController1 from Viewcontroller with parameters in ViewController1 With valid data back to Viewcontroller after the data is selected, the delegate implements the above functions.Project: Click to downloadFirst,viewcontroller main codebutton click Event-(ibaction) CLICKSEARCHBTN: (ID) sender {[[[ uiapplication sharedapplication] Keywindow] endediting:yes]; ViewController1 *_
Since C # hosting Code does not contain pointers, when you want to pass a function as a parameter to another function, you cannot use the normal method, as a proxy (delegate) is created, the proxy in C # is similar to the function pointer. In addition to implementing the function pointer, it has other features.Let's talk about the function pointer in C ++:
Void printnum (INT num) {cout
Void (* funptr) (INT );// Declare a function pointer
Funptr = pri
It is permissible to pass a method M of Class A to another object of Class B, so that the object of Class B can call this method M, which is plainly the method can be passed as a parameter.classProgram {//How to use delegate one Public Delegate voidUsersstringName,intAge ); Public Static voidUserstringNameintAge ) {Console.WriteLine ("Name:"+name+"\ n Age:"+Age ); Console.readkey (); }
block instead of delegate, use block as much as possible, and consider using protocol for a large number of delegate methods.1.Block syntax summary and examples are as follows:1. Block mode of common code blocksReturnType (^blockname) (parametertypes) = ^returntype (parameters) {Block code};Use no example:Int (^ABC) (int a) = ^int (int a) {return a+1;};int AA = ABC (2);NSLog (@ "%d", AA);2. Attribute Mode b
If the unmanaged code needs to call the callback in the managed code multiple times, save the reference of the delegate as a member variable. Otherwise, an exception similar to the following occurs:
CallbackOnCollectedDelegate DetectedMessage: For "Demo! SomeNamespace. SomeClass + SomeDelegate: Invoke "type of garbage collection delegate for callback. This may cause application crash, damage, and data loss
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.