Skip to content

nullable-shorthand

C#stylefix: safedefault: offcsharp/stylecopcsharp/roslynator

Reports Nullable<int> or System.Nullable<int>, which is int?. The check is by name: a type of your own called Nullable is reported too. It is not reported in using aliases, in nameof, in typeof(Nullable<>), and before a member access (Nullable<int>.Equals). The fix writes int?.

int? is the form of the language and is shorter. One spelling makes the nullable types easy to find, and Nullable<T> stays for the places where the generic name is the point.

Reported
class A {
int a, b;
Nullable<int> n = (long)1 < 2 ? null : null;
void F(object o, int i) {
var f = delegate (int x) { return x; };
var g = null == o || 1 < i;
...
After the fix
class A {
int a, b;
int? n = (long)1 < 2 ? null : null;
void F(object o, int i) {
var f = delegate (int x) { return x; };
var g = null == o || 1 < i;
...
csharp:
lint:
rules:
nullable-shorthand: warn # off (default), warn, error

Enabled by the presets: csharp/stylecop, csharp/roslynator.

Suppress one line with sudsy-disable-next-line nullable-shorthand in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
Roslynator RCS1020 full
StyleCop SA1125 full