Skip to content

no-default-value-type-constructor

C#stylefix: safedefault: offcsharp/stylecop

Reports new S() of a value type that has an implicit parameterless constructor and no initializer: write default(S), or the named constant where there is one (Guid.Empty, CancellationToken.None, IntPtr.Zero, the enum member with value 0). No fix for a target-typed new().

new S() of such a struct is the zero value, the same as default(S), but it reads as a call that does something. default states that the value is the zero value, and a named constant (Guid.Empty) says what that value means.

To fix, write default(S) or the constant.

Reported
...
var s = new S();
var d = new DateTime();
...
Debug.Assert(true, "why");
...
After the fix
...
var s = default(S);
var d = default(DateTime);
...
Debug.Assert(true, "why");
...
csharp:
lint:
rules:
no-default-value-type-constructor: warn # off (default), warn, error

Enabled by the presets: csharp/stylecop.

Suppress one line with sudsy-disable-next-line no-default-value-type-constructor in a comment of the file’s language.

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

Tool Rule Match
StyleCop SA1129 full