[Analyzer Proposal]: Don't include non-IEquatable
structs in records
#102593
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-System.Runtime
code-analyzer
Marks an issue that suggests a Roslyn analyzer
Milestone
Uh oh!
There was an error while loading. Please reload this page.
Background and motivation
Using records in C# can be tricky (performance-wise) if the developer isn't aware of some implementation details. The compiler generates the equality implementation using
EqualityComparer<T>.Default
, which isObjectEqualityComparer
for a struct that is notIEquatable<T>
.Using
ObjectEqualityComparer
will cause boxing for every equality operation being done.API Proposal
An analyzer that warns if a record primary constructor has a parameter which is a value type (struct) that doesn't implement
IEquatable<T>
whereT
is the same value type.For example:
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: