Example of a delegate in. NET 2.0

Source: Internet
Author: User
Because. NET 2.0 introduces anonymous methods, the way you create a delegate can be simplified.

Delegates in. NET 2.0

With the anonymous method , the previous example can be simplified to:

1 namespace Delegatedemo 2  {3     //Declaration Delegate 4 Public      delegate void Mydel (string arg1, string arg2);  5  6     class program 7       {8         static void Main (string[] args) 9           {Ten             //.net 2.0 delegates each              //create a delegate , using the anonymous method                 Mydel Mydel = Delegate (string arg1, String arg2),                    {                        Console.WriteLine (string). Format ("Arg1:{0},arg2:{1}", Arg1, Arg2)),                      };17 ("             aaa", "BBB")          , Mydel//Call delegation             Console.readkey ();          +                   }           

As you can see, it is not necessary to define the type and method separately, just use the inline syntax to implement it.

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.