rspec/rules/S3237/csharp/rule.adoc
2022-02-04 16:28:24 +00:00

61 lines
1.3 KiB
Plaintext

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.
== Noncompliant Code Example
[source,csharp]
----
private int count;
public int Count
{
get { return count; }
set { count = 42; } // Noncompliant
}
----
== Compliant Solution
[source,csharp]
----
private int count;
public int Count
{
get { return count; }
set { count = value; }
}
----
or
[source,csharp]
----
public int Count
{
get { return count; }
set { throw new InvalidOperationException(); }
}
----
== 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.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]