About C # delegated application and summary,

Source: Internet
Author: User

About C # delegated application and summary,
What is delegation?Official explanation

A delegate is a type that defines the method signature. When instantiating a delegate, You can associate its instance with any method with a compatible signature. You can call a method by entrusting an instance.

  Personal UnderstandingA delegate is an object that holds one or more methods. The object can be executed and can be passed. Usage  First, create a class or multiple classes and use the keyword delegat to create a delegate instantiation class. Use the delegate class to create a delegate object and assign a value to the object. the "+" symbol can hold multiple methods to call the delegate call method, which is similar to the function class. I will write it in this case: // namespace using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace ConsoleApp3 // defines two public class dogs {public void play () {Console. writeLine ("Wang") ;}} public class cat {public void play () {Console. writeLine ("meow ") ;}} Class Program {delegate void act (); // defines a delegate Type static void Main (string [] args) {dog = new dog (); // instantiate the object cat = new cat (); act del = dog. play; // define the delegate object del + = cat. play; // hold multiple methods del + = delegate () // lambda expression {Console. write ("what are you doing! ") ;}; Da (); Console. Read ();}}}

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.