Advertisement
Teodor92

15. Generics

Jan 13th, 2013
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class GenericMethods
  5. {
  6.     static T MinOfSet<T>(params T[] numArray)
  7.     {
  8.         dynamic min = numArray[0];
  9.         for (int i = 0; i < numArray.Length; i++)
  10.         {
  11.             if (numArray[i] < min)
  12.             {
  13.                 min = numArray[i];
  14.             }
  15.         }
  16.         return min;
  17.     }
  18.     static T MaxOfSet<T>(params T[] numArray)
  19.     {
  20.         dynamic max = numArray[0];
  21.         for (int i = 0; i < numArray.Length; i++)
  22.         {
  23.             if (numArray[i] > max)
  24.             {
  25.                 max = numArray[i];
  26.             }
  27.         }
  28.         return max;
  29.     }
  30.     static T AverageOfSet<T>(params T[] numArray)
  31.     {
  32.         dynamic sum = 0;
  33.         for (int i = 0; i < numArray.Length; i++)
  34.         {
  35.             sum = sum + numArray[i];
  36.         }
  37.         return (sum / numArray.Length);
  38.     }
  39.     static T SumOfSet<T>(params T[] numArray)
  40.     {
  41.         dynamic sum = 0;
  42.         for (int i = 0; i < numArray.Length; i++)
  43.         {
  44.             sum = sum + numArray[i];
  45.         }
  46.         return sum;
  47.     }
  48.     static T ProductOfSet<T>(params T[] numArray)
  49.     {
  50.         dynamic product = 1;
  51.         for (int i = 0; i < numArray.Length; i++)
  52.         {
  53.             product = product * numArray[i];
  54.         }
  55.         return product;
  56.     }
  57.     static void Main()
  58.     {
  59.         Console.WriteLine("The minimum of set of ints is: {0}", MinOfSet(1, 2, 3, 4, -5, 6, 7));
  60.         Console.WriteLine("The maximum of set of ints is: {0}", MaxOfSet(1, 2, 3, 4, -5, 6, 7));
  61.         Console.WriteLine("The average of set of ints is: {0}", AverageOfSet(2,3));
  62.         Console.WriteLine("The sum of set of ints is: {0}", SumOfSet(1, 2, 3, 4, -5, 6, 7));
  63.         Console.WriteLine("The product of set of ints is: {0}", ProductOfSet(1, 2, 3, 4, -5, 6, 7));
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement