All rules
CA2019Reliability Enabled by default: As suggestion

ThreadStatic fields should not use inline initialization

ThreadStatic fields should not use inline initialization

Microsoft docs

Description

System.ThreadStaticAttribute fields should be initialized lazily on use and not with inline initialization or explicitly in a static (Shared in Visual Basic) constructor. A static constructor only initializes the field on the thread that runs the type's static constructor.

Cause

A field that's annotated with System.ThreadStaticAttribute is initialized inline or explicitly in a static (Shared in Visual Basic) constructor.

Example

class C
{
    [ThreadStatic]
    private static Object obj = new();
}

class C
{
    [ThreadStatic]
    private static Object obj;

    static void S1()
    {
        obj ??= new Object();
    }
}

When to suppress

It's safe to suppress a warning from this rule, but your app might exhibit unexpected behavior.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0