Object-oriented (delegate)

Source: Internet
Author: User

Commissioned:
Also known as the agent, the event is also a delegate;
Defined at the outermost of the class

1. Define the delegate
Keyword: delegate
Function Signature: Signature and function remain consistent
When defining a delegate, you define it according to the function.
public delegate int first (int a,int b);
The return type of the method pointed to requires the parameters to be consistent!

2. Defining the delegate variable, pointing to the method

A delegate cannot be instantiated because it is not a class;

First F = new Jiafa; New delegate variable, point to method, note!! method does not need parentheses!!

The second time you can use + =

public int Jiafa (int a,int b)
{
return a+b;
}

Call:
f (5,3);

Can be understood as a pointer to a function, which function the delegate points to, which function is represented by the delegate
Allows functions to be passed as arguments

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceConsoleApplication18 {9     //single-row modeTen     //control a class can instantiate only one object One  A     //class Test -     //{ -     //public string name; the     //} -  -     //Data Access Classes -     classDbda +     { -          Public stringhost; +          Public stringdatabase; A  at         //a static member that is used to store the object of the class -          Public StaticDBDA db =NULL; -  -         //let the class not be instantiated -         PrivateDbda () -         { in         } -  to         //provide a way to create objects, control only one object +          Public Staticdbda Duixiang () -         { the             if(db = =NULL) *             { $db =NewDbda ();Panax Notoginseng             } -  the             returndb; +         } A     } the  +     //Defining Delegates -      Public Delegate voidSuibian (strings); $  $     class Program -     { -         Static voidMain (string[] args) the         { -            //Test T1 = new test ();Wuyi            //Test t2 = new test (); the  -             //Dbda db = Dbda. Duixiang (); Wu             //db.host = "localhost"; -             //Dbda db1 = Dbda. Duixiang (); About  $  -             //Delegate -             //parameterization of the method -Suibian s =China ; A  +s + = America;//+ = is the binding method = minus a method the  -             //Events $             //events are a special kind of delegate the  the  the             //calling a speech method the Speak (s); -  in             the  the Console.WriteLine (); About console.readline (); the  the  the             //Object-oriented three major features +             //Design Patterns -         } the Bayi         //methods of speech function the         Static voidSpeak (Suibian yu) the         { -Yu"Zhang San"); -  the             //if (n = = 0) the             //{ the             //America (); the             //} -             //else if (n = = 1) the             //{ the             //China (); the             //}94             //else if (n = = 2) the             //{ the             //Hanyu (); the             //}98      About         } - 101         //Voice Pack102          Public Static voidAmerica (strings)103         {104Console.WriteLine (s+"Hello"); the         }106          Public Static voidChina (strings)107         {108Console.WriteLine (s+"Hello");109         } the          Public Static voidHanyu (strings)111         { theConsole.WriteLine (s+"Bjdkaj");113         } the  the    the     }117}

Object-oriented (delegate)

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.