Introduction to delegates in C #

Source: Internet
Author: User


What is a delegate?

Previously wrote an introduction to the event:

This also summarizes the relevant knowledge of the Commission.

Delegates are type-safe in C # and can subscribe to one or more function pointers with the same signature method

How to declare a delegate: Delegate return value type delegate type name (parameter)

Like what:


The code is as follows:


delegate void Stringprocess (string s);

Note: In addition to the previous delegate, the remainder is the same as declaring a function, but stringprocess is not a function name, but a delegate type name

A declared delegate is a type, just like an int, a person, if you want to declare a variable of a delegate type, declare the delegate type variable in the same way: Stringprocess F1;

Point the delegate type variable to the function stringprocess sp = new stringprocess (SayHello) so that you can use the SP as a function as you would call a normal function. A delegate can be seen as a pointer to a function. An integer can point to it with an integer variable, an object can point to it with an object variable, and a function can point to it with a delegate variable. And the difference between the direct call function: The delegate can point to any function, even if it is not defined before, but not the use of those limited.

The delegate-type variable-pointing function can also be simplified to stringprocess sp = SayHello, and the compiler helps us with new. But you can't sp=printit (), because it's a function call.


The code is as follows:


Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using System.Threading.Tasks;

Namespace Wolfy.delegatetest

{

Class Program

{

delegate void Stringprocess (string s);

static void Main (string[] args)

{

string s = "Hi,i AM wolfy";

String s2 = "How does it?";

Defining a Delegate variable p

Stringprocess p = new stringprocess (ToLower);

Simplified approach

stringprocess P2 = ToLower;

P (s);

P2 (S2);

Console.read ();

}

static void ToLower (string s)

{

Console.WriteLine (S.tolower ());

}

}

}

Results:


The code is as follows:


stringprocess P2 = ToLower;

What does the compiler do for us in this simplified way? You can look at it by using the Anti-compilation tool:

Can see the compiler help us new one.

The return type and signature Specify the form of the accepted method of the delegate: that is, what is your delegate's style, and the method that points to it, for example, where the return type is the void parameter is of type string.


Anonymous methods

When using delegate, it is often not necessary to use an ordinary method, because this method is only used by this delegate, and only once, it is most appropriate to use the anonymous method.

Anonymous methods are methods that do not have a name. 3 is an int object without a name. 3+5 is the addition of two anonymous int objects, allowing anonymous methods to be allowed.


The code is as follows:


Processworddelegate p = Delegate (string s)

{

Console.WriteLine (s);

};

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Introduction to delegates in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23209.html






Related content C # implementation Google Translate API sample code C # Four ways to format GUID in how to resolve hash conflicts C # Connection database method
C # access to SQL Server Set link timeout method C # Methods for traversing system processes C # Implementation of IP Camera methods C # Network Programming basics Process and threading

Introduction to delegates in C #

Related Article

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.