C # advanced programming (version 8th)-delegate statement and use

Source: Internet
Author: User

First, declare a Currency structure. The Currency structure has its own ToString () overload method and a static method with the same signature as GetCurrencyUnit. In this way, you can use the same delegate variable to call these methods:

 struct Currency    {        public uint Dollars;        public ushort Cents;        public Currency(uint dollars, ushort cents)        {            this.Dollars = dollars;            this.Cents = cents;        }        public override string ToString()        {            return string.Format("${0}.{1,-2:00}", Dollars, Cents);        }        public static string GetCurrencyUnit()        {            return "Dollar";        }        public static explicit operator Currency(float value)        {            checked            {                uint dollars = (uint)value;                ushort cents = (ushort)((value - dollars) * 100);                return new Currency(dollars, cents);            }        }        public static implicit operator float(Currency value)        {            return value.Dollars + (value.Cents / 100.0f);        }        public static implicit operator Currency(uint value)        {            return new Currency(value, 0);        }        public static implicit operator uint(Currency value)        {            return value.Dollars;        }    }

The following code is a GetString instance:

 private delegate string GetAString();        static void Main()        {            int x = 40;            GetAString firstStringMethod = x.ToString;            Console.WriteLine("String is {0}", firstStringMethod());            Currency balance = new Currency(34, 50);            // firstStringMethod references an instance method            firstStringMethod = balance.ToString;            Console.WriteLine("String is {0}", firstStringMethod());            // firstStringMethod references a static method            firstStringMethod = new GetAString(Currency.GetCurrencyUnit);            Console.WriteLine("String is {0}", firstStringMethod());        }

This Code illustrates how to call a method by Delegate, and then re-assign the delegate different methods referenced on different instances of the class, or even specify the specified static method, or the method referenced on different types of instances of the class. As long as the signature of each method matches the delegate definition.

Running the above program will get the output results of different methods referenced by the delegate:

String is 40

String is $34.50.

String is Dollar

However, we have not actually explained the specific process of passing a delegate to another method, nor have we obtained any particularly useful results. Calling the ToString () method of the int and Currency objects is much more intuitive than using the delegate !!! However, a complicated example is needed to describe the nature of the Delegate, so as to truly understand the usefulness of the delegate. In the next section, two examples of delegation are provided. In the first example, only two different operations are called using a delegate. He explained how to pass the delegate to the method and how to use the delegate array, but there is still no good description: without the delegate, you cannot do a lot of work. The second example is much more complicated. It has a BubbleSorter class which implements a method to sort an object array in ascending order. It is difficult to compile this class without delegation !!! (For details, see the next section)

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.