C # One of the delegate usage

Source: Internet
Author: User


C # One of the delegate usage

Chang jianzhao

A while ago I discussed how to call another winform method in a winform. The best way to do this is to use delegate. Some friends also mentioned in the reply, google has sent a message, so I am sorry for the last time I used it. But fortunately, I still learned the knowledge. It may be a joke for others, but for myself, is a kind of progress. Haha, I don't want to compare myself with others, I just want to surpass myself ..

 

Okay, let's talk about it in a few minutes. Now, let's put some of my understanding of delegate to everyone's pp. If there are any mistakes, please correct them.

 

Note:There are manyDelegateA lot of detailed information is written. This is just my ownDelegateThe usage is not necessarily correct. Don't blame me for misleading you.

 

One search on the Internet, the most popular introduction to delegate in Chinese is the translation of Stanley B. Lippman (author of C # primer. Give a link so that you can refer to the link. Http://www.chinaaspx.com/article/csharp/355.htm (provided by Chinese DOTNET Club)

 

 

First, let's take a look at the definition of delegate:

A delegate Declaration defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static or an instance method. delegates are roughly similar to function pointers in C ++; however, delegates are type-safe and secure.

-- Transfer fromMsdn website

 

The above definition explains that a delegate Declaration defines a reference type, which is used to compress a method in a specified form. A delegate instance compresses a static or instance method. Delegates and function pointers in C ++ are similar. However, the difference is that delegate is safe and type-safe.

 

When I first looked at this definition, I felt strange, because at that time I had no idea what type-safe was, and my younger brother had never touched C ++. Besides knowing that there was a pointer, I don't know anything. Fortunately, there are Google brothers. The two definitions below should help us better understand the delegate.

These two definitions are found on the codeproject website.

C # delegate is a callback function. In other words, delegate is a way to provide feedback from Class-server to class-client.

C #'s delegate is a callback function. In other words, delegate is a way to provide feedback from the class server to the class client.

C # delegate is smarter then "standard" Callback because it allows defining a strict list of parameters which are passed from Class-server to class-Client

However, C # delegate is smarter than general callback functions. Because it allows defining a strict parameter list. The parameter list contains the parameters passed from the class server to the class client.

In Lippman's article, he explains the differences between C # Delegate and C ++ pointers. I personally think that the explanation is very good and it is helpful for understanding the delegate. This post will help you "Digest ".



C #'s delegate type

Delegate is a function pointer, but compared with common function pointers, there are three main differences:

1) A delegate object can carry multiple methods at a time, instead of one at a time. When we call a delegate carrying multiple methods, all methods are called in order of the order in which they are loaded with delegate objects. Let's take a look at how to do this.

2) The method (methods) carried by a delegate object does not need to belong to the same category. All methods (methods) 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 provide a set of public methods for querying the delegate object or its carrying method (methods)

 

The following describes the definition and usage of delegate by calling an instance in another window:

Define delegate:
Public void delegate formhandler (string MSG, buddy)

 

 

 

Create instance:
Formhandler = new formhandler (string MSG, buddy );

 

 

 

 

Target:

 

Formhandler = new formhandler (formshow );

 

 

 

 

How to display form:

 

Public void formshow (string MSG, buddy)


{


If (! This. invokerequired) // if the current thread is not the main UI thread, true is returned. Otherwise, false is returned.



{


Chatform cf = new chatform (MSG, buddy );


Cf. Show ();

}


Else


{


Formhandler fhandler = new formhandler (showform); // create a new instance pointing to formshow.



This. begininvoker (showform, new object [] {MSG, buddy}); // start delegate



}


}

The above program can probably see how to define and start a delegate. I successfully implemented the function of calling another window in one window using the above method. If you have any questions, you can send me an email for discussion.

 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.