C # delegated Learning (1)

Source: Internet
Author: User
Public class class1
{
Delegate int deleadd (int A, int B); // delegate statement
Delegate int deleremove ();

Public class1 ()
{
}

Public static void start ()
{
Class1 C1 = new class1 ();

Deleadd add = new deleadd (c1.add); // instantiate the delegate and specify the delegate Method
Add + = new deleadd (c1.add1); // delegate another method

Deleremove remove = NULL;
Remove + = new deleremove (c1.remove); // initialize two methods of delegation (1, new 2, + = new)
Remove + = new deleremove (c1.remove1 );

MessageBox. Show (add (). tostring (); // display 30 (displayed in the Add1 method), display 30 again (returned by the function)

MessageBox. Show (remove (). tostring (); // display "not added", "not added", 100
}

Private int add (int A, int B)
{
Return A + B;
}

Private int Add1 (int A, int B)
{
MessageBox. Show (convert. tostring (a + B ));
Return A + B;
}
Private int remove ()
{
MessageBox. Show ("not added ");
Return 0;
}

Private int remove1 ()
{
MessageBox. Show ("the same is not added ");
Return 100;
}
}

Public class form
{
Class1.start ();
}

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.