Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(obj == null)
- throw new ArgumentNullException()
- public class Validatable<T>
- {
- public T Value { get; }
- public Validatable(ref T argument)
- {
- Value = argument;
- }
- }
- public static Validatable<T> ThrowIf<T>(this T argument)
- {
- return new Validatable<T>(ref argument);
- }
- public static Validatable<T> IsNull<T>(this Validatable<T> argument) where T : class
- {
- if (argument.Value == null)
- throw new ArgumentNullException();
- return argument;
- }
- public static Validatable<T> CollectionEmpty<T>(this Validatable<T> argument) where T: ICollection
- {
- if (argument.Value.Count == 0)
- throw new ArgumentException();
- return argument;
- }
- List<int> testList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- testList.ThrowIf().IsNull().CollectionEmpty();
- int iNumber = 1000000000;
- Stopwatch sw1 = new Stopwatch();
- Stopwatch sw2 = new Stopwatch();
- sw1.Start();
- for (int i = 0; i < iNumber; ++i)
- {
- if (testList == null)
- throw new ArgumentNullException();
- if (testList.Count == 0)
- throw new ArgumentException();
- }
- sw1.Stop();
- sw2.Start();
- for (int i = 0; i < iNumber; ++i)
- {
- testList.ThrowIf().IsNull().CollectionEmpty();
- }
- sw2.Stop();
- Console.WriteLine($"sw1: {sw1.ElapsedTicks} ticks");
- Console.WriteLine($"sw2: {sw2.ElapsedTicks} ticks");
- public static void IsNullLite<T>(this T argument)
- {
- if (argument == null)
- throw new ArgumentException();
- }
- public static void CollectionEmpty2Lite<T>(this T argument) where T : ICollection
- {
- if (argument.Count == 0)
- throw new ArgumentException();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement