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
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: - -
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
In custom user controls, we often add custom events, and then we use the delegate (delegate).
In development, it is often used to login. Then, we make the login interface into a control, it is more convenient to use it later.
The login interface is simple, do it casually, it has a "login" button. We need to click the "Login" button, it can perform our other pages defined by the login operation, then nee
Http://www.cnblogs.com/xiaofei59/archive/2010/11/25/1887285.html#]
Exception {the anonymous method cannot be converted to the type "System. Delegate" because it is not the Delegate type}
The delegate actually uses the method name as a parameter. However, if there are multiple methods, you must specify the parameter.
Check the following code:This. Invoke (
Exception {the anonymous method cannot be converted to the type "system. Delegate" because it is not the delegate type}
The delegate actually uses the method name as a parameter. However, if there are multiple methods, you must specify the parameter.
View the following information:Code:This. Invoke (delegate{Messag
C # is a controversial emerging language developed and created by Microsoft as the cornerstone of Visual Studio. NET. It is currently in the release phase of the first Beta version. C # combines many features from C ++ and Java. The main criticism of the Java Community for C # Is that it claims that C # is just a lame Java clone version-rather than a result of language innovation, it is a result of litigation. In the C ++ community, the main criticism (also targeting Java) is, C # Is just anothe
[. NET Basics]-delegate, event, thread (3),. net delegate
In the previous two articles, we have learned about delegation and events. In this article, we will take a look at the thread.1. A form program has one thread by default (equivalent to a store with only one clerk). This default thread is called the UI thread/main thread. 2. Relationship between processes and threads:
A. A process contains the resourc
Delegation, events, and Observer Design Patterns
Example
The above example is not enough for the following explanation. Let's take a look at a new example. Because we have already introduced a lot of content, the progress in this section will be slightly faster:
If we have a high-end water heater, we can power it on. When the water temperature exceeds 95 degrees: 1. The speaker will start to send a voice to tell you the temperature of the water; 2. the LCD screen will also change the display of
Using delegates:Can be thought of as declaring a protocol in a class, and another class as a delegate to implement the method within this protocol.For example, when creating a table in the iphone, you need to use the UITableView class. However, this class does not know the title of the table, does not know how many parts or rows need to be displayed, and does not know what should be displayed in each row or cell in the table. So it delegates this seri
JQuery event delegate instance analysis and jquery delegate instance analysis
Event delegation is mainly implemented by event bubbling. precise understanding of event delegation can help improve code execution efficiency. Let's take a look at a code example:
In the above Code, the bind () method is used to bind a click event handler function for each td, so that when you click a cell, the text in the cell
IntroductionArticles about events and delegates have seen a lot, but they are always incomplete. Especially when explaining to others is always very fragmented, so in this sort of, but also for everyone's reference, very please enlighten us. This article from how a subclass triggers the base class of events (event), leads to the commonality and distinction between events and delegates. After a brief analysis of the reasons behind the underlying reasons, we also provide some reference materials.
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.