C # Delegate Base 1--Delegation Foundation

Source: Internet
Author: User
Tags readline

C # commissioned the basic series was originally published in February 2011 in my Sina blog, now it will be like this blog.

the delegate and its delegate methods must have the same signature. Same signature: 1. Same parameter type 2. Same number of parameters 3. Return values consistent

Example One

Class program
{public
        delegate int Mathmethod (int x, int y);
       
        public int Add (int a, int b)
        {return
            a + b;
        }

        static void Main (string[] args)
        {
            mathmethod mm;
            Program P = new program ();
            MM = p.add;//is equivalent to the container Console.WriteLine of a Method
            ("evaluates to {0}", mm (7,6));
            Console.ReadLine ();
        }

Case II

Class Program {public delegate double Mathmethod (double x, double y);
        Double Add (double A, double b) {return a + B;
        Double subtract (double A, double b) {return a + B;
        Double Multiply (double A, double b) {return a * b;
        Double Divide (double A, double) {return A/b;
            } void Docalculate (Mathmethod mm) {Console.WriteLine ("Please enter first number");

            Double x = convert.todouble (Console.ReadLine ());
            Console.WriteLine ("Please enter a second number");

            Double y = convert.todouble (Console.ReadLine ());
            Console.WriteLine ("Result {0}", mm (x, y));
           
        Console.ReadLine ();
            } static void Main (string[] args) {mathmethod mm;
            Program P = new program ();
            MM = P.divide;
        P.docalculate (mm); }
}


This article refers to the contents of the agent from Jin Xu-liang's ". NET 4.0 object-oriented Programming".

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.