Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. // Andrew Vasilyev, 2010
  2. using System;
  3.  
  4. namespace rubytrolling
  5. {
  6.     public static class IntExtension
  7.     {
  8.         public static void Times(this int value, Action action)
  9.         {
  10.             if (action == null) throw new ArgumentNullException("action");
  11.             for (var i = 0; i < value; i++)
  12.                 action();
  13.         }
  14.  
  15.         public static void UpTo(this int value, int to, Action<int> action)
  16.         {
  17.             if (action == null) throw new ArgumentNullException("action");
  18.             for (var i = value; i <= to; i++)
  19.                 action(i);
  20.         }
  21.  
  22.         public static void DownTo(this int value, int to, Action<int> action)
  23.         {
  24.             if (action == null) throw new ArgumentNullException("action");
  25.             for (var i = value; i >= to; i--)
  26.                 action(i);
  27.         }
  28.     }
  29.  
  30.     class Program
  31.     {
  32.         static void Main()
  33.         {
  34.             10.Times(() => Console.WriteLine("Hello"));
  35.             5.UpTo(10, Console.WriteLine);
  36.             10.DownTo(5, Console.WriteLine);
  37.  
  38.             1.UpTo(5,
  39.                 i => 1.UpTo(5,
  40.                     j => Console.WriteLine(i * j)
  41.                 )
  42.             );
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement