C # differences between delegate and event,
In general, a delegate is a class that maintains a field internally and points to a method. An event can be considered as a delegate type variable. Multiple delegates or methods can be registered or canceled through the event. This document describes the differences between t
Because of the project needs, C # needs to call C + + libraries for programming.Today in the process of debugging, suddenly popped up ....The garbage collection delegate for the type has a callback. This can cause application crashes, corruption, and data loss. When you pass a delegate to unmanaged code, the managed ap
I. Entrusted
When we need to pass the method as a parameter to other methods, we need to use the delegate.
Because sometimes the object we want to manipulate is not the data, but the operation of a method.
Let's just start with the code.
using System; namespace gosoa.com.cn { public class test { public delegate string GetAString(); public static void Main() { int x=10;
syntax of anonymous methods. It is easy to convert an anonymous method to a lambda expression by following these steps.
Delete delegate keyword
Put the lambda operator between the argument list and the anonymous method BODY = =
The following example demonstrates a lambda expressionCalcdelegate myadddelegate = (intint y) + = { + y); } ; = (in
SummaryDelegation is a very important concept in C # programming, and it is also a difficult point. This article will explain the system details of the delegation.1. What is a delegate?In fact, I have been thinking about how to explain the Commission in order to make the delegation more thorough. To tell the truth, everyone has a different opinion because of the angle of the problem. Personally, it can be u
Event is an important concept in Windows-based programming. In almost all Windows applications, a large number of asynchronous calls are involved, such as responding to and Clicking buttons and processing Windows system messages. These asynchronous calls must be completed through events. It is no exception even on the next-generation development platform --. NET.So what is an event? An event is a message sent from an object. The message indicates a specific action or a specific condition. For
references direct to other methods are added to the delegate, these references are stored in the delegate call list, this type of delegation is called multi-channel broadcast delegation. All C # delegates are implicit multicast delegates. To add multiple method references to the call list of a delegate, you can call a
following results:75Purpose of the delegate (Delegate)The following example demonstrates the use of a delegate. The delegate printstring can be used to refer to a method with a string as input and does not return anything.We use this de
, arg2));}31 public static void Staticmethod (string arg1, String arg 2) {Console.WriteLine (string. Format ("Arg1:{0},arg2:{1}", Arg1, arg2)); 35}36 }37}
From the above code can be summed up:
1, a delegate can accept either an instance method or a static method, as long as the signature and return value type of the method matches the delegate.
2, there are two ways to invoke a
are added to the delegate, these references are stored in the delegate call list, this type of delegation is called multi-channel broadcast delegation. All C # delegates are implicit multicast delegates. To add multiple method references to the call list of a delegate, you can call all methods at a time through the
".
Ii. Delegation
If you have a C/C ++ background, the function pointer is the best description delegate. for average people, it can be considered as a way to pass methods like common parameters. for example, the following three lines of code reflect the same basic principles:Process MethodPass a piece of data wit
: 1classmathsoperations2{3 Public Static DoubleMultiplyBy2 (Doublevalue)4 {5 returnValue *2;6 }7with such a method, we can instantiate a delegate: Mathsop Operation=NewMathsop (MATHSOPERATIONS.MULTIPLYBY2); When instantiating a delegate, give it a parameter, which is the method of the delegate execution, which can be either a static method or an
delegate4 Delegate intMyDelegate (intXinty);5 6 Static voidMain (string[] args)7 {8 //instantiating a delegate9 //1. Use the New keywordTenMyDelegate _mydelegate =Newmydelegate (getsum); One A //2. Using anonymous Methods -MyDelegate mydelegate =Delegate(intXinty) - { the returnX +y; - }; - - /
From the preface, we should know a little bit about the importance of the Commission and the event, although we can still blur it, but from my plain English series, I will make these concepts easy to understand. First of all, we should first talk about the Commission, literally understand, as long as the Chinese should know this meaning, unless the commission of the 2 characters do not know, for example, Xiao Ming entrusted to buy a ticket.But is it s
// ================================================ ====================================// TITLE:// C ++ vs c # (7): pointer to and delegate to a function// AUTHOR:// Norains// DATE:// Thursday 17-jaruary-2011// Environment:// Visual Studio 2010// Visual Studio 2005
// Modify// AM, Thursday 17-January-2011 first draft// PM, Thursday 17-January-2011 modify the met
In C #, a delegate type is a type-safe, object-oriented function pointer. When we define a delegate type through the delegate keyword, the compiler generates three methods for the delegate type: Invoke, BeginInvoke, and EndInvoke.For exa
delegate constructor usage.1 , delegating the use of constructorsTo learn more about the new features of c++11, we can use the following example to see how this is done:Program source code (DELEGATINGCTOR1.CPP)Class a{PublicA (int i) is the delegate constructor for a ()A (): a (0) {}A (int i, int j) is a
Delegates belong to the term in C # and are widely used, for example, events are the simplest and most straightforward examples of delegates.So first of all to say what is a delegate, in fact, the delegate in the use of C or C + +
// ================================================ ====================================// Title:// C ++ vs C # (7): pointer to and delegate to a function// Author:// Norains// Date:// Thursday 17-jaruary-2011// Environment:// Visual Studio 2010// Visual Studio 2005
// Modify// AM, Thursday 17-January-2011 first draft// Pm, Thursday 17-January-2011 modify the met
Delegate:
In msdn, the first sentence about delegation is to associate the delegation with explicit or anonymous methods to instantiate the delegation. C ++ has a very powerful stuff-pointer. Of course, this is also a lot of criticism. C # has no pointer, but many times we need the powerful functions of C ++, but we ca
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.