In this cases:
private class C
{
private bool aBool = false;
private int i = 0;
private DateTime d = DateTime.MinValue;
private MyClass mc = null;
private MyStruct ms = default(MyStruct);
//etc...
}
Becomes:
private class C
{
private bool aBool;
private int;
private DateTime d;
private MyClass mc;
private MyStruct ms;
//etc...
}
This analyzer applies to all fields which are not marked const. For each field f, let T be the declared type of f. If f is initialized with the value default(T), then report this diagnostic. The following additional cases should be handled:
- If
T is a reference type or a nullable value type, then treat null the same as default(T). Note: this means you can always report this diagnostic for f = null, regardless of the actual type T.
- If
T is a numeric primitive type, then treat 0 (or equivalent double or float) value as default(T).
- If
T is an enum type, then treat 0 as equivalent to default(T). Note: do NOT treat an enum member this way, even if the value of the enum member is 0.
- If
T is IntPtr, then treat IntPtr.Zero equivalent to default(T).
- If
T is UIntPtr, then treat UIntPtr.Zero equivalent to default(T).
- If
T is DateTime, then treat DateTime.MinValue equivalent to default(T).
Should use WellKnownDiagnosticTags.Unnecessary.
Severity: Info.
Diagnostic id is CC0034.
Category: Usage
In this cases:
Becomes:
This analyzer applies to all fields which are not marked
const. For each field f, letTbe the declared type of f. If f is initialized with the valuedefault(T), then report this diagnostic. The following additional cases should be handled:Tis a reference type or a nullable value type, then treatnullthe same asdefault(T). Note: this means you can always report this diagnostic forf = null, regardless of the actual typeT.Tis a numeric primitive type, then treat0(or equivalent double or float) value asdefault(T).Tis an enum type, then treat0as equivalent todefault(T). Note: do NOT treat an enum member this way, even if the value of the enum member is 0.TisIntPtr, then treatIntPtr.Zeroequivalent todefault(T).TisUIntPtr, then treatUIntPtr.Zeroequivalent todefault(T).TisDateTime, then treatDateTime.MinValueequivalent todefault(T).Should use
WellKnownDiagnosticTags.Unnecessary.Severity:
Info.Diagnostic id is
CC0034.Category:
Usage