C # Delegate of Reading notes

Source: Internet
Author: User

The use of C # proxies is primarily when you need to pass one method as a parameter to another method.

For example, start a thread to perform a task, and the method that the thread executes can be passed through the proxy.

A proxy consists of a method or multiple methods whose address is similar to a C + + function pointer, but it is type-safe.

1. Declaring an agent

delegate void Intmethodinvoker (int x);

This proxy can refer to the return value type is empty, there is a parameter for all methods, it is important to know that delegate is type safe.

Delegate Double Twolongsop (long first, long second);

There are two parameters that return a proxy with a double value.

Delegate string getastring ();

No arguments return a proxy with a value of string.

Public delegate string getastring ();

You can also describe the delegate's access rights.

2. Using proxies

The use of a proxy must first instantiate a proxy

Pri v a te delegate string getastring ();
static void Main ()
{
int x = 40;
getAString Firststringmethod = new getAString (x.tostring); Instantiate an agent and point the agent to X.tostring ();
Console.WriteLine ("String is {0}", Firststringmethod ()); Firststringmethod () becomes x.tostring () representative (agent)
With Firststringmethod initialized to X.tostring (),
The above statement is equivalent to saying
Console.WriteLine ("String is {0}", x.tostring ());
}

Not to be continued ...

C # Delegate of Reading notes

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.