C #2.0 delegate and anonymous delegate for new feature inquiry

Source: Internet
Author: User

Delegate is often used to compare with callback. In fact, the two have many things in common to some extent. However, there are many more powerful delegate features.

First, you can register any number of callbacks in delegate. When a delegate is called, the registered process is called one by one.

Second, delegate allows you to register an object method. Unlike the static method or global method in C ++, delegate provides more flexibility and implies that we, delegate stores a lot of object information in some way.

In the anonymous delegate of C #2.0, we can even access the context variable of the current anonymous delegate. In the next article, we will look at the powerful delegate through actual examples.

First, let's look at a typical delegate Writing Method in C #1.2.

Public delegate void TheEvent (int );
Public void test ()
{
TheEvent testdel1 = new TheEvent (del1 );
Testdel1 (12 );
}

Public void del1 (int x)
{
Console. WriteLine ("output x: {0}", x );
}

Now we can write it as follows:

Public void test ()
{
TheEvent testdel1 = del1;
Testdel1 (12 );
}

Or rewrite the program:

Delegate void TheEvent2 (int );
Public void test2 ()
{
Int a = 12;
TheEvent ev2 = delegate (ref int x)
{Console. WriteLine ("output x: {0}", x );};
Ev2 (ref );
}

Compared with 1.2, delegate has better readability, but does it seem to have no essential improvement? Slow down. Let's take a look at the example below.

Public static void test3 ()
{
Int a = 12;
Int y = 32;
TheEvent ev2 = delegate (ref int x)
{Console. WriteLine ("output x + y: {0}", x + y );};
Ev2 (ref );
}

Note: Content in anonymous functions! The value of x + y is output correctly, but in 1.2, the Delegate does not allow access to local variables except parameters. What are the advantages of doing so?

Let's look at a more complex example:

Public static void test4 ()
{
Int a = 12;
Int y = 32;
TheEvent ev2 = delegate (ref int x)
{Console. WriteLine ("output x + y: {0}", x + y); Thread. Sleep (100 );};
// Ev2 (ref );
IAsyncResult ar = ev2.BeginInvoke (ref,
Delegate (IAsyncResult ar2)
{Console. write ("Operation finished: {0} on thread ID: {1}, is pool: {2}", ar2.IsCompleted, Thread. currentThread. getHashCode (), Thread. currentThread. isThreadPoolThread );}
, Null );

Console. WriteLine ("do some other calculations while counter thread is working ");
Console. Write ("work status: {0} Main Thread ID: {1}, is pool: {2 }",
Ar. IsCompleted,
Thread. CurrentThread. GetHashCode (),
Thread. CurrentThread. IsThreadPoolThread );
Thread. Sleep (500 );

Ev2.EndInvoke (ref a, ar );
}

In this example, the system thread pool is used to queue tasks, which is suitable for IO or computing-intensive operations. The biggest advantage of using anonymous delegation is that it can completely clone the available variables in the context of the current running space, although this may also increase the synchronization complexity from another layer, so there is a loss.

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.