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
;//here, the NC actually binds 2 delegates, which are executed in the order in which they are bound . - //Call Multicast thenc5); *Console.WriteLine ("Value of Num: {0}", Getnum ()); $ Console.readkey ();Panax Notoginseng } - } the}Note : Delegate objects can be merged using the "+ =" operator. A merge delegate invokes the two delegates it merges. Only delegates of the same type can be mer
method, multiple methods with the same signature it is called in serial mode at the same time. The description of multicast delegation will be explained later.
Invoke Method
To explainInvokeMethod. Let's review how a delegate calls a method,CodeAs follows:
Int c = This. MD (A, B );
When we call a delegate and execute the method pointed to by t
Sometimes, we want to call a delegate, but we can execute multiple methods at the same time (the most common in custom events), for example, a jobCompositionAfter the file is generated, the system will generate the document log and save it to the database. For the above two operations, if you only want to call one delegate, you can complete it in sequence, you ca
Question: The difference between delegate and delegateAnalysis:I believe many friends in the development process have encountered the above questions, in order to solve this question, recently consulted a lot of posts, and finally in the official MSDN document found the answer.Here are the official document link addresses for delegate and delegateDelegate: https://msdn.microsoft.com/zh-cn/library/ms173171.a
{Ten class Program One { A Static voidMain (string[] args) - { - //No delegate with no parameter return value the -Action Action1 =delegatetest; - action (); - + Console.readkey (); - } + A Static voiddelegatetest () at { -Console.WriteLine ("delegate without a parameter"); - } - } -}2.Func:A de
corresponding delegate, but the problem is that For the thread class that itself has been overloaded to use ThreadStart and Parameterizedthreadstart (here is an example, of course, for all of these cases), the incoming method group causes the compiler to give an error:Thread t=New// Compiler Error: Method invocation has ambiguityThe same situation cannot be used to convert a method group directly
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:
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
The essence of the chain is that you put a bunch of things to execute in a list, when you want to trigger a set of things, you do not need to write one by one (such as the factory canteen dinner, this method is implemented, to let the chef a time in the cafeteria waiting to play rice, b staff at a certain time line, b staff may more than one , should be notified that Class C staff at another time in line to eat, the last Class D cleaners at another ti
) carried by a delegate object must have the same prototype and form. However, these methods can be either static or non-static and can be composed of one or more members of different categories.
3) A delegate type statement creates a new subtype instance, which is derived from. net Library Framework abstract base classes delegate or multicastdelegate, they pro
A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C ++. unlike C function pointers, delegates are object-oriented, type safe, and secure. the type of a delegate is defined by the name of the d
type of the method must be the same as that of the delegate.
Calling a delegate is equivalent to calling the method bound to the delegate. A delegate can bind multiple methods. The delegate is a reference type, so it can be passed everywhere. Without it, can
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.