ellapt

T9.15.GenericMethods

Jan 21st, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. using System;
  2.  
  3. class GenericMethods
  4. {
  5. static T Minimum<T>(params T[] numbers)
  6. {
  7. dynamic minNum = numbers[0];
  8. int length = numbers.Length;
  9. for (int i = 0; i < length; i++)
  10. {
  11. if (numbers[i] < minNum)
  12. {
  13. minNum = numbers[i];
  14. }
  15. }
  16. return minNum;
  17. }
  18. static T Maximum<T>(params T[] numbers)
  19. {
  20. dynamic maxNum = numbers[0];
  21. int length = numbers.Length;
  22. for (int i = 0; i < length; i++)
  23. {
  24. if (numbers[i] > maxNum)
  25. {
  26. maxNum = numbers[i];
  27. }
  28. }
  29. return maxNum;
  30. }
  31.  
  32. static T Average<T>(dynamic sum, params T[] numbers)
  33. {
  34. int cnt = numbers.Length;
  35. return sum/cnt;
  36. }
  37.  
  38. static T Sum<T>(params T[] numbers)
  39. {
  40. dynamic sum = 0;
  41. foreach (T number in numbers)
  42. {
  43. sum = sum + number;
  44. }
  45. return sum;
  46. }
  47.  
  48. static T Product<T>(params T[] numbers)
  49. {
  50. dynamic product = 1;
  51. foreach (T number in numbers)
  52. {
  53. product *= number;
  54. }
  55. return product;
  56. }
  57.  
  58. static void Main()
  59. {
  60. Console.WriteLine("Modify the CalcIntSetNumbers class for any number type");
  61. Console.WriteLine("(integer, decimal, float, byte, etc.) using generic method ");
  62.  
  63. /* Console.WriteLine("Minimum value in the set is: {0}", Minimum(1.5, 2.5, -4, 10, 12));
  64. Console.WriteLine("Maximum value in the set is: {0}", Maximum(1.5, 2.5, -4, 10, 12));
  65. double sum = Sum(1.5, 2.5, -4, 10, 12);
  66. Console.WriteLine("Sum of the elements in the set is: {0}", sum);
  67. Console.WriteLine("Average of the elements in the set is: {0}", Average(sum, 1.5, 2.5, -4, 10, 12));
  68. Console.WriteLine("Product of the elements in the set is: {0}", Product(1.5, 2.5, -4, 10, 12));
  69.  
  70. Console.WriteLine("Minimum value in the set is: {0}", Minimum(444444444444444444,888888888888888887,2.0));
  71. Console.WriteLine("Maximum value in the set is: {0}", Maximum(444444444444444444,888888888888888887,2.0));
  72. dynamic sum = Sum(444444444444444444,888888888888888887,2.0);
  73. Console.WriteLine("Sum of the elements in the set is: {0}", sum);
  74. Console.WriteLine("Average of the elements in the set is: {0}", Average(sum,444444444444444444,888888888888888887,2.0));
  75. Console.WriteLine("Product of the elements in the set is: {0}", Product(444444444444444444,888888888888888887,2.0));
  76. */
  77. Console.WriteLine("Minimum value in the set is: {0}", Minimum(98, 100, 2.0, 5));
  78. Console.WriteLine("Maximum value in the set is: {0}", Maximum(98, 100, 2.0, 5));
  79. dynamic sum = Sum(98, 100, 2.0, 5);
  80. Console.WriteLine("Sum of the elements in the set is: {0}", sum);
  81. Console.WriteLine("Average of the elements in the set is: {0}", Average(sum, 98, 100, 2.0, 5));
  82. Console.WriteLine("Product of the elements in the set is: {0}", Product(98, 100, 2.0, 5));
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment