rspec/rules/S2996/csharp/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

75 lines
1.9 KiB
Plaintext

== Why is this an issue?
When an object has a field annotated with ``++ThreadStatic++``, that field is shared within a given thread, but unique across threads. Since a class' static initializer is only invoked for the first thread created, it also means that only the first thread will have the expected initial values.
Instead, allow such fields to be initialized to their default values or make the initialization lazy.
=== Noncompliant code example
[source,csharp]
----
public class Foo
{
[ThreadStatic]
public static object PerThreadObject = new object(); // Noncompliant. Will be null in all the threads except the first one.
}
----
=== Compliant solution
[source,csharp]
----
public class Foo
{
[ThreadStatic]
public static object _perThreadObject;
public static object PerThreadObject
{
get
{
if (_perThreadObject == null)
{
_perThreadObject = new object();
}
return _perThreadObject;
}
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this initialization of "xxx" or make it lazy.
'''
== Comments And Links
(visible only on this page)
=== on 3 Jun 2015, 16:04:50 Ann Campbell wrote:
\[~tamas.vajk] could you supply the code samples please? I think I could stumble through a basic example, but IMO we need to show 2 compliant versions: one with no initializer and the other with lazy initialization & I have little confidence about getting the latter right.
=== on 8 Jun 2015, 08:57:45 Tamas Vajk wrote:
\[~ann.campbell.2] I've added the code sample
=== on 8 Jun 2015, 12:02:46 Ann Campbell wrote:
Thanks [~tamas.vajk]
=== on 15 Jun 2015, 12:12:42 Tamas Vajk wrote:
\[~ann.campbell.2] I've removed the "that's a good thing" remark from the description.
=== on 15 Jun 2015, 14:00:47 Ann Campbell wrote:
okay, fine [~tamas.vajk]
endif::env-github,rspecator-view[]