sashomaga

Extension Methods Interface

Feb 25th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public static class Test
  7. {
  8.     static void Main()
  9.     {
  10.  
  11.         List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
  12.  
  13.         Console.WriteLine(numbers.ToString<int>());
  14.  
  15.         numbers.IncreaseWidth(5);
  16.  
  17.         Console.WriteLine(numbers.ToString<int>());
  18.  
  19.     }
  20. }
  21.  
  22. public static class IListExtensions
  23. {
  24.  
  25.     public static void IncreaseWidth(
  26.  
  27.           this IList<int> list,
  28.  
  29.           int amount)
  30.     {
  31.  
  32.         for (int i = 0; i < list.Count; i++)
  33.         {
  34.  
  35.             list[i] += amount;
  36.  
  37.         }
  38.  
  39.     }
  40.  
  41. }
  42.  
  43. public static class IEnumerableExtensions
  44. {
  45.  
  46.     public static string ToString<T>(
  47.  
  48.           this IEnumerable<T> enumeration)
  49.     {
  50.  
  51.         StringBuilder result = new StringBuilder();
  52.  
  53.         result.Append("[");
  54.  
  55.         foreach (var item in enumeration)
  56.         {
  57.  
  58.             result.Append(item.ToString());
  59.  
  60.             result.Append(", ");
  61.  
  62.         }
  63.  
  64.         if (result.Length > 1)
  65.  
  66.             result.Remove(result.Length - 2, 2);
  67.  
  68.         result.Append("]");
  69.  
  70.         return result.ToString();
  71.  
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment