Exploring the .NET CoreFX Part 4: The Requires Convenience Class
Exploring the .NET CoreFX .net core csharp system.collections.immutable
Published: 2014-11-20
Exploring the .NET CoreFX Part 4: The Requires Convenience Class

This is part 4/17 of my Exploring the .NET CoreFX series.

The System.Collections.Immutable project in the .NET CoreFX includes a convenience class called Requires, which looks like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
internal static class Requires
{
    [DebuggerStepThrough]
    public static T NotNull([ValidatedNotNull]T value, string parameterName)
        where T : class // ensures value-types aren't passed to a null checking method
    {
        if (value == null)
        {
            throw new ArgumentNullException(parameterName);
        }

        return value;
    }

    ...
}

This allows other methods to write code like:

1
2
3
4
5
6
public void Foo(string bar)
{
    Requires.NotNull(bar);

    ...
}