Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. if(obj == null)
  2. throw new ArgumentNullException()
  3.  
  4. public class Validatable<T>
  5. {
  6. public T Value { get; }
  7. public Validatable(ref T argument)
  8. {
  9. Value = argument;
  10. }
  11. }
  12.  
  13.  
  14. public static Validatable<T> ThrowIf<T>(this T argument)
  15. {
  16. return new Validatable<T>(ref argument);
  17. }
  18.  
  19. public static Validatable<T> IsNull<T>(this Validatable<T> argument) where T : class
  20. {
  21. if (argument.Value == null)
  22. throw new ArgumentNullException();
  23. return argument;
  24. }
  25.  
  26. public static Validatable<T> CollectionEmpty<T>(this Validatable<T> argument) where T: ICollection
  27. {
  28. if (argument.Value.Count == 0)
  29. throw new ArgumentException();
  30. return argument;
  31. }
  32.  
  33. List<int> testList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  34. testList.ThrowIf().IsNull().CollectionEmpty();
  35.  
  36. int iNumber = 1000000000;
  37. Stopwatch sw1 = new Stopwatch();
  38. Stopwatch sw2 = new Stopwatch();
  39. sw1.Start();
  40. for (int i = 0; i < iNumber; ++i)
  41. {
  42. if (testList == null)
  43. throw new ArgumentNullException();
  44. if (testList.Count == 0)
  45. throw new ArgumentException();
  46. }
  47. sw1.Stop();
  48. sw2.Start();
  49. for (int i = 0; i < iNumber; ++i)
  50. {
  51. testList.ThrowIf().IsNull().CollectionEmpty();
  52. }
  53. sw2.Stop();
  54.  
  55. Console.WriteLine($"sw1: {sw1.ElapsedTicks} ticks");
  56. Console.WriteLine($"sw2: {sw2.ElapsedTicks} ticks");
  57.  
  58. public static void IsNullLite<T>(this T argument)
  59. {
  60. if (argument == null)
  61. throw new ArgumentException();
  62. }
  63.  
  64. public static void CollectionEmpty2Lite<T>(this T argument) where T : ICollection
  65. {
  66. if (argument.Count == 0)
  67. throw new ArgumentException();
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement