Generic delegation in C #

Source: Internet
Author: User
  1. Using system;
  2. Using system. collections;
  3. Using system. Collections. Generic;
  4. Using system. LINQ;
  5. Using system. text;
  6. Namespace generic
  7. {
  8. // Define the delegate: tinput is the object to be accumulated. tsummary is the type to be returned.
  9. Class generic Method
  10. {
  11. Public static void main ()
  12. {
  13. // Add all accounts to the list set
  14. List <account> List = new list <account> ();
  15. List. Add (New Account ("Aladdin", 1000 ));
  16. List. Add (New Account ("zhaohaifu", 2000 ));
  17. List. Add (New Account ("Jacky", 3000 ));
  18. // Computing Summary
  19. Decimal amount = algorithm. accumulate <account, decimal> (list, mydelimpl );
  20. Console. writeline (amount );
  21. // For example, the function compute sum with the check function is used. If the sum is lower than 1500, the calculation is not performed.
  22. Decimal checkednum = algorithm. checkedaccumulate <account, decimal> (A => A. Balance> 1500, list, mydelimpl );
  23. Console. writeline ("less than 1500 does not participate in calculation:" + checkednum );
  24. Console. Readline ();
  25. }
  26. // Delegate implementation method
  27. Public static decimal mydelimpl (account A, decimal D)
  28. {
  29. Return A. Balance + D;
  30. }
  31. }
  32. // Computing class
  33. Class Algorithm
  34. {
  35. // Define the delegate. If tinput is used to calculate the accumulated content, tsummary is the type to be returned.
  36. Public Delegate tsummary action <tinput, tsummary> (tinput input, tsummary sum );
  37. // Two parameters, one is the content e to be added to the class, and the other is the delegate type action.
  38. Public static tsummary accumulate <tinput, tsummary> (ienumerable <tinput> E, Action <tinput, tsummary> action)
  39. {
  40. // Initialization value
  41. Tsummary tsum = default (tsummary );
  42. Foreach (tinput I in E)
  43. {
  44. // Call the delegate method cyclically to accumulate the value.
  45. Tsum = action (I, tsum );
  46. }
  47. Return tsum;
  48. }
  49. // Calculation Method for adding the check function
  50. Public Delegate bool checkmyaccount <tinput> (tinput t );
  51. Public static tsummary checkedaccumulate <tinput, tsummary> (checkmyaccount <tinput> check, ienumerable <tinput> E, Action <tinput, tsummary> action)
  52. {
  53. Tsummary rest = default (tsummary );
  54. Foreach (tinput I in E)
  55. {
  56. If (check (I ))
  57. {
  58. // Call the delegate method cyclically to accumulate the value.
  59. Rest = action (I, rest );
  60. }
  61. }
  62. Return rest;
  63. }
  64. }
  65. // Account type
  66. Class account
  67. {
  68. Private string name;
  69. Public string name
  70. {
  71. Get {return name ;}
  72. Set {name = value ;}
  73. }
  74. Private decimal balance;
  75. Public decimal balance
  76. {
  77. Get {return balance ;}
  78. Set {balance = value ;}
  79. }
  80. Public Account (string name, decimal balance)
  81. {
  82. This. Name = Name;
  83. This. Balance = balance;
  84. }
  85. }
  86. }

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.