namespace Example
{
public ref struct Test
{
byte _data;
}
}
CA1066:
namespace Example
{
public ref struct Test
{
byte _data;
public override bool Equals(object obj) => false;
}
}
The problems
CA1815 doesn't make sense because Equals must always return false, since ref structs can't be cast to object.
CA1066 doesn't make sense because ref structs can't implement interfaces.
CA2231 doesn't make sense without also exposing an Equals method. It's possible to implement operator == and operator !=, but it's probably not a great idea to promote that as the only equality checking method (which is what would happen if CA1815 and CA1066 were removed).
Proposed solution
Exclude ref structs from CA1815.
Exclude ref structs from CA1066 and anything else that recommends implementing an interface.
Exclude ref structs from CA2231, as the wording and proposed fix don't make sense for ref structs.
Add an analyzer that recommends adding either public bool Equals(RefType other) or public static bool Equals(RefType left, RefType right)
Add an analyzer that recommends overriding == and != if either public bool Equals(RefType other) or public static bool Equals(RefType left, RefType right) are present.
Analyzer package
Microsoft.CodeQuality.Analyzers from Microsoft.CodeAnalysis.FxCopAnalyzers
Package Version
2.9.1
Diagnostic ID
Repro steps
CA1815 and CA2231:
CA1066:
The problems
object
.Proposed solution
public bool Equals(RefType other)
orpublic static bool Equals(RefType left, RefType right)
public bool Equals(RefType other)
orpublic static bool Equals(RefType left, RefType right)
are present.