rspec/rules/S3237/csharp/rule.adoc

43 lines
997 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
In property and indexer ``++set++`` methods, and in event ``++add++`` and ``++remove++`` methods, the implicit ``++value++`` parameter holds the value the accessor was called with. Not using the ``++value++`` means that the accessor ignores the caller's intent which could cause unexpected results at runtime.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
private int count;
public int Count
{
get { return count; }
set { count = 42; } // Noncompliant
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
private int count;
public int Count
{
get { return count; }
set { count = value; }
}
----
or
----
public int Count
{
get { return count; }
set { throw new InvalidOperationException(); }
}
----
2021-04-28 16:49:39 +02:00
== Exceptions
This rule doesn't raise an issue when the setter is empty and part of the implementation of an ``++interface++`` . The assumption is that this part of the interface is not meaningful to that particular implementation. A good example of that would be a "sink" logger that discards any logs.