Advertisement
stefanpu

Max Method Implementation

Dec 17th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1. [__DynamicallyInvokable]
  2.  
  3. //The object, calling this method, is passed as an argument,
  4. // although this is not visible when we use code.
  5. public static int Max(this IEnumerable<int> source)                        
  6. {
  7.     if (source != null)
  8.     {
  9.         int num = 0;
  10.         bool flag = false;
  11.         foreach (int num1 in source)
  12.         {
  13.             if (!flag)
  14.             {
  15.                 num = num1;
  16.                 flag = true;
  17.             }
  18.             else
  19.             {
  20.         //Key moment
  21.         //----------------
  22.                 if (num1 <= num) //Here, the tested value must be greater than current max value.
  23.                 {
  24.                     continue;
  25.                 }
  26.                 num = num1;
  27.         //--------------------------
  28.             }
  29.         }
  30.         if (!flag)
  31.         {
  32.             throw Error.NoElements();
  33.         }
  34.         else
  35.         {
  36.             return num;
  37.         }
  38.     }
  39.     else
  40.     {
  41.         throw Error.ArgumentNull("source");
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement