Skip to content

Latest commit

 

History

History
55 lines (40 loc) · 1.58 KB

File metadata and controls

55 lines (40 loc) · 1.58 KB

SA1125

TypeName SA1125UseShorthandForNullableTypes
CheckId SA1125
Category Readability Rules

Cause

The Nullable type has been defined not using the C# shorthand. For example, Nullable<DateTime> has been used instead of the preferred DateTime?

Rule description

A violation of this rule occurs whenever the Nullable type has been defined without using the shorthand C# style.

This rule applies to the value-type nullable syntax only. In a #nullable enable context, long-form value type usages such as Nullable<int> or System.Nullable<DateTime> still report SA1125 and should be written as int? or DateTime?. Nullable reference type annotations like string? or T? are unaffected by this rule.

Examples

#nullable enable

// The following fields will report SA1125 and should use the shorthand form
private Nullable<int> count;
private System.Nullable<DateTime> timestamp;

// These nullable reference type annotations are valid and do not require changes for SA1125
private string? name;
private T? value;

How to fix violations

To fix a violation of this rule, use the shorthand version of the nullable type ie. int?, DateTime?, etc.

How to suppress violations

[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1125:UseShorthandForNullableTypes", Justification = "Reviewed.")]
#pragma warning disable SA1125 // UseShorthandForNullableTypes
#pragma warning restore SA1125 // UseShorthandForNullableTypes