Search Results for

    Show / Hide Table of Contents

    Null Constraint

    NullConstraint tests that a value is null. This is one of the most commonly used constraints for verifying that references have not been assigned or have been explicitly set to null.

    Usage

    Is.Null
    Is.Not.Null
    

    Examples

    [Test]
    public void NullConstraint_Examples()
    {
        object? obj = null;
        Assert.That(obj, Is.Null);
    
        string name = "Alice";
        Assert.That(name, Is.Not.Null);
    
        // Combining with other constraints
        object? result = "value";
        Assert.That(result, Is.Not.Null.And.Not.EqualTo(""));
    }
    

    Notes

    1. For value types, consider using Is.Default instead, which tests for the default value of the type.
    2. When testing nullable value types (int?, bool?, etc.), Is.Null works as expected.

    See Also

    • Default Constraint
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0