# Null

`ShouldBeNull` and `ShouldNotBeNull` allow you to check whether a value is null.

`ShouldNotBeNull` returns the non-null value if it succeeds so that further assertions can be chained. When used with a reference type, the returned value is the same reference annotated as non-null. Equivalently, when used on a `System.Nullable<T>` expression, the returned value is the unwrapped `T` value.

## ShouldBeNull

```cs
var myRef = "Hello World";
myRef.ShouldBeNull();
```

[snippet source](https://github.com/shouldly/shouldly/blob/master/src/DocumentationExamples/CodeExamples/ShouldBeNullNotNullExamples.ShouldBeNull.codeSample.approved.cs#L1-L2) | [anchor](#snippet-ShouldBeNullNotNullExamples.ShouldBeNull.codeSample.approved.cs)

**Exception**

```
myRef
    should be null but was
"Hello World"
```

### ShouldBeNull (nullable value type)

```cs
int? nullableValue = 42;
nullableValue.ShouldBeNull();
```

[snippet source](https://github.com/shouldly/shouldly/blob/master/src/DocumentationExamples/CodeExamples/ShouldBeNullNotNullExamples.NullableValueShouldBeNull.codeSample.approved.cs#L1-L2) | [anchor](#snippet-ShouldBeNullNotNullExamples.NullableValueShouldBeNull.codeSample.approved.cs)

**Exception**

```
nullableValue
    should be null but was
42
```

## ShouldNotBeNull

```cs
string? myRef = null;
myRef.ShouldNotBeNull();
```

[snippet source](https://github.com/shouldly/shouldly/blob/master/src/DocumentationExamples/CodeExamples/ShouldBeNullNotNullExamples.ShouldNotBeNull.codeSample.approved.cs#L1-L2) | [anchor](#snippet-ShouldBeNullNotNullExamples.ShouldNotBeNull.codeSample.approved.cs)

**Exception**

```
myRef
    should not be null but was
```

### ShouldNotBeNull (nullable value type)

```cs
int? myRef = null;
myRef.ShouldNotBeNull();
```

[snippet source](https://github.com/shouldly/shouldly/blob/master/src/DocumentationExamples/CodeExamples/ShouldBeNullNotNullExamples.NullableValueShouldNotBeNull.codeSample.approved.cs#L1-L2) | [anchor](#snippet-ShouldBeNullNotNullExamples.NullableValueShouldNotBeNull.codeSample.approved.cs)

**Exception**

```
myRef
    should not be null but was
```

## ShouldNotBeNull with chaining

```cs
var myRef = (string?)"1234";
myRef.ShouldNotBeNull().Length.ShouldBe(5);
```

[snippet source](https://github.com/shouldly/shouldly/blob/master/src/DocumentationExamples/CodeExamples/ShouldBeNullNotNullExamples.ShouldNotBeNullWithChaining.codeSample.approved.cs#L1-L2) | [anchor](#snippet-ShouldBeNullNotNullExamples.ShouldNotBeNullWithChaining.codeSample.approved.cs)

**Exception**

```
myRef.ShouldNotBeNull().Length
    should be
5
    but was
4
```

### ShouldNotBeNull with chaining (nullable value type)

```cs
SomeStruct? nullableValue = new SomeStruct { IntProperty = 41 };
nullableValue.ShouldNotBeNull().IntProperty.ShouldBe(42);
```

[snippet source](https://github.com/shouldly/shouldly/blob/master/src/DocumentationExamples/CodeExamples/ShouldBeNullNotNullExamples.NullableValueShouldNotBeNullWithChaining.codeSample.approved.cs#L1-L2) | [anchor](#snippet-ShouldBeNullNotNullExamples.NullableValueShouldNotBeNullWithChaining.codeSample.approved.cs)

**Exception**

```
nullableValue.ShouldNotBeNull().IntProperty
    should be
42
    but was
41
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.shouldly.org/documentation/equality/null.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
