Validation Extension method performance I'm writing a simple set/framework of extension methods for argument/parameter validation. What I'm mostly concerned about, ATM is the performance, against the simpliest boilerplate
if(obj == null)
   throw new ArgumentNullException()


The framework I came up with is like that:
The Validatable class packing the base object that will be validated through the ThrowIf() extension method.
public class Validatable&lt;T&gt;
{
    public T Value { get; }
    public Validatable(ref T argument)
    {
        Value = argument;
    }
}


public static Validatable&lt;T&gt; ThrowIf&lt;T&gt;(this T argument)
{
    return new Validatable&lt;T&gt;(ref argument);
}

And test methods themselves:
public static Validatable&lt;T&gt; IsNull&lt;T&gt;(this Validatable&lt;T&gt; argument) where T : class
{
    if (argument.Value == null)
        throw new ArgumentNullException();
    return argument;
}

public static Validatable&lt;T&gt; CollectionEmpty&lt;T&gt;(this Validatable&lt;T&gt; argument) where T: ICollection
{
    if (argument.Value.Count == 0)
        throw new ArgumentException();
    return argument;
}

This allows me to validate items with simple:
List&lt;int&gt; testList = new List&lt;int&gt;() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
testList.ThrowIf().IsNull().CollectionEmpty();

Everything was fine untill I decided to test, how it fares against the simple boilerplate.
int iNumber = 1000000000;
Stopwatch sw1 = new Stopwatch();
Stopwatch sw2 = new Stopwatch();
sw1.Start();
for (int i = 0; i &lt; iNumber; ++i)
{
    if (testList == null)
        throw new ArgumentNullException();
    if (testList.Count == 0)
        throw new ArgumentException();
}
sw1.Stop();
sw2.Start();
for (int i = 0; i &lt; iNumber; ++i)
{
    testList.ThrowIf().IsNull().CollectionEmpty();
}
sw2.Stop();

Console.WriteLine($&quot;sw1: {sw1.ElapsedTicks} ticks&quot;);
Console.WriteLine($&quot;sw2: {sw2.ElapsedTicks} ticks&quot;);

and the output was:

sw1: 10720019 ticks
sw2: 161726524 ticks

That's an order of magnitute difference. It was over 1,000,000,000 iterations, but still the difference is there.
I also tried with
public static void IsNullLite&lt;T&gt;(this T argument)
{
    if (argument == null)
        throw new ArgumentException();
}

public static void CollectionEmpty2Lite&lt;T&gt;(this T argument) where T : ICollection
{
    if (argument.Count == 0)
        throw new ArgumentException();
}

And it's result came somewhere in the middle with

sw3: 84165637 ticks

My questions are:

Should I be concerned with the performance loss. It was +- 3sec vs 47secs on my PC, at 1,000,000,000 iterations. I do not plan to run it in such loops, over so many objects, but I was going to use it my game engine, where I have about 16ms per Update tick, to do much more than just validate some variables.
Is there a better/faster way of doing it. Preferably, I'd like to pack the validation object into some sort of Validatable class, to not clutter the intelisense with dozens of validation methods.
Is there some error on my part, in the way I tested it? I ran the tests in Debug mode with code optimalization turned off.
public static Validatable&lt;T&gt; ThrowIf&lt;T&gt;(this T argument)
{
    return new Validatable&lt;T&gt;(ref argument);
}



This of course is creating a lot of objects. You could speed this a little bit up by making Validatable a struct but still, it'll be slower then the normal check. Convenience comes often with some kind of a performance hit. 




  I was going to use it my game engine


You'd better not. Make it fast, not convenient.

There is one more thing about validating arguments. The only thing you need to validate is user input or something that you cannot predict like configurations. Internally most methods usually work without argument checking because it's nearly impossible to pass invalid values. Use Debug.Assert for this purpose during development to be absolutely sure if you want. It'll be removed in the release version so that it doesn't slow the application down.