"C # advanced" Multicast delegates and delegate arrays like it's one thing ~

Source: Internet
Author: User

This mathoperation class has three static methods, the parameters are double, and there is no return value, consider using action<> () This predefined delegate OH

    classmathoperations { Public Static voidMultiplybytwo (Doublevalue) {Console.WriteLine ("0} = {1}", Value,value *2); }         Public Static voidSquare (Doublevalue) {Console.WriteLine ("{0}* {1} = {2}", value, value, value*value); }         Public Static voidIntroduction (Doublevalue) {Console.WriteLine ("I ' m a double. My value is {0}", value); }    }

The book mentions the delegate array , and we define one. Put three methods inside.

            action<double>[] Delegatelist =            {                mathoperations.multiplybytwo,                Mathoperations.square,                mathoperations.introduction            };

I call this delegate this way.

            foreach (var in delegatelist)            {                del (5);            }

Output:

5* 5 = 25
5 = 10
I ' m a double. My value is 5

Try the multicast delegate + number again.

            action<Double> operations = mathoperations.multiplybytwo;             + = Mathoperations.square            ; + = mathoperations.introduction;            Operations (5);

The result is the same.

But the book says it's a good idea.

         Public Static void Processanddisplaynumber (action<doubledouble  value)        {            operations (value);        }

Give a chestnut, hit, relatives, curse, pinch people, these 4 actions all need a person as your object of action. I am an action entrusted <in person, you can let me hit, Curse, Curse * *, all of a sudden to perform a number of actions, including repetitive movements. That's what I understand, at the moment.

"C # advanced" Multicast delegates and delegate arrays like it's one thing ~

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.