rspec/rules/S3898/csharp/rule.adoc

29 lines
641 B
Plaintext
Raw Normal View History

2023-01-26 10:24:38 +01:00
If you're using a `struct`, it is likely because you're interested in performance. But by failing to implement `IEquatable<T>` you're loosing performance when comparisons are made because without `IEquatable<T>`, boxing and reflection are used to make comparisons.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
struct MyStruct // Noncompliant
{
2023-01-26 10:24:38 +01:00
public int Value { get; set; }
2021-04-28 16:49:39 +02:00
}
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
struct MyStruct : IEquatable<MyStruct>
{
2023-01-26 10:24:38 +01:00
public int Value { get; set; }
2021-04-28 16:49:39 +02:00
2023-01-26 10:24:38 +01:00
public bool Equals(MyStruct other)
{
// ...
}
2021-04-28 16:49:39 +02:00
}
----
2023-01-26 10:24:38 +01:00
include::../see.adoc[]
include::../rspecator.adoc[]