C # Delegate

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace consoleapplication8
{
// Define the delegate
Public Delegate bool comparedelegate (int A, int B );
Public class mycompare
{
/// <Summary>
/// Define a method that meets the requirements of the delegate defined above. The return value is bool type and the two parameters are int type respectively.
/// </Summary>
/// <Param name = "X"> </param>
/// <Param name = "Y"> </param>
/// <Returns> </returns>
Public static bool comparemethod (int x, int y)
{
Bool result = x> Y? True: false;
Return result;
}

}
Class Program
{

Static void main (string [] ARGs)
{
// Instantiate the delegate and initialize it. Note that a method name must be included during initialization, and the method must comply with the signature (parameter type and return value) when the delegate is initially defined)
Comparedelegate mydelegate = new comparedelegate (mycompare. comparemethod );
// Now we use a delegated instance to compare the size of two numbers A and B
Int A = 10;
Int B = 15;
Bool isbigger = mydelegate (A, B );
Console. writeline ("A> B? : "+ Isbigger );
Console. Read ();
}
}
}

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.