66 lines
1.2 KiB
Plaintext
66 lines
1.2 KiB
Plaintext
== Why is this an issue?
|
|
|
|
When a non-``++static++`` class field is annotated with ``++ThreadStatic++``, the code seems to show that the field can have different values for different calling threads, but that's not the case, since the ``++ThreadStatic++`` attribute is simply ignored on non-``++static++`` fields.
|
|
|
|
|
|
So ``++ThreadStatic++`` should either be removed or replaced with a use of the ``++ThreadLocal<T>++`` class, which gives a similar behavior for non-``++static++`` fields.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,csharp]
|
|
----
|
|
public class MyClass
|
|
{
|
|
[ThreadStatic] // Noncompliant
|
|
private int count = 0;
|
|
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,csharp]
|
|
----
|
|
public class MyClass
|
|
{
|
|
private int count = 0;
|
|
|
|
// ...
|
|
}
|
|
----
|
|
or
|
|
|
|
[source,csharp]
|
|
----
|
|
public class MyClass
|
|
{
|
|
private readonly ThreadLocal<int> count = new ThreadLocal<int>();
|
|
public int Count
|
|
{
|
|
get { return count.Value; }
|
|
set { count.Value = value; }
|
|
}
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
|
|
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[]
|