81 lines
2.7 KiB
Plaintext
81 lines
2.7 KiB
Plaintext
== Why is this an issue?
|
|
|
|
When you need to get external input for `set` and `init` methods defined for properties and indexers or for `remove` and `add` methods for events, you should
|
|
always get this input throught the `value` contextual keyword.
|
|
|
|
The contextual keyword `value` is similar to an input parameter of a method; it references the value that the client code is attempting to assign to the property, indexer or event.
|
|
|
|
The keyword `value` holds the value the accessor was called with. Not using it means that the accessor ignores the caller's intent which could cause unexpected results at runtime.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
private int count;
|
|
public int Count
|
|
{
|
|
get { return count; }
|
|
set { count = 42; } // Noncompliant
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
----
|
|
private int count;
|
|
public int Count
|
|
{
|
|
get { return count; }
|
|
set { count = value; }
|
|
}
|
|
----
|
|
|
|
|
|
=== 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.
|
|
|
|
|
|
== Resources
|
|
=== Documentation
|
|
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties[Properties]
|
|
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value[Value keyword]
|
|
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/add[Add keyword]
|
|
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/remove[Remove keyword]
|
|
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/set[Set keyword]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Use the "value" contextual keyword in this [property set | property init |indexer set|event] accessor.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 8 Jul 2015, 13:51:34 Ann Campbell wrote:
|
|
\[~tamas.vajk] 10min seems like a high remediation cost. Does that mean that the Compliant Solution I added is off-base?
|
|
|
|
=== on 20 Jul 2015, 11:59:05 Tamas Vajk wrote:
|
|
\[~ann.campbell.2] We can reduce the required time.
|
|
|
|
=== on 20 Jul 2015, 13:53:16 Ann Campbell wrote:
|
|
I halved it to 5min, [~tamas.vajk]
|
|
|
|
=== on 27 Jul 2015, 15:39:03 Ann Campbell wrote:
|
|
\[~dinesh.bolkensteyn] note that I've edited the first line. Your version, "a property and indexer ``++set++`` method" speaks of one, collective method for both a property and an indexer at one time.
|
|
|
|
=== on 27 Jul 2015, 15:44:58 Dinesh Bolkensteyn wrote:
|
|
thanks [~ann.campbell.2]
|
|
|
|
endif::env-github,rspecator-view[]
|