Skip to content

BRO1115: Use shorthand for nullable types

int? instead of Nullable<int>. Replaces StyleCop SA1125.

Example

Before

using System;
using System.Collections.Generic;

public class Reading
{
    public Nullable<double> Value { get; set; }

    public List<System.Nullable<int>> Samples { get; } = new List<System.Nullable<int>>();
}

After

using System;
using System.Collections.Generic;

public class Reading
{
    public double? Value { get; set; }

    public List<int?> Samples { get; } = new List<int?>();
}

Why

T? is the form everyone reads at a glance, and the one the compiler and IDE show.

How the fix works

Only System.Nullable<T> itself is changed (a type of your own named Nullable<T> isn't), qualified or not, also inside other type arguments, typeof(...) and default(...). Nested ones are done in one pass (Nullable<KeyValuePair<int, Nullable<long>>> becomes KeyValuePair<int, long?>?).

Not reported

  • Places where only the long form works or means something else: typeof(Nullable<>), nameof(Nullable<int>), Nullable<int>.Equals(...), crefs, using aliases, and is/as operands.

Compared with StyleCop

Same positions as StyleCop. StyleCop has no fix for SA1125.