CA2259: Ensure ThreadStatic
is only used with static fields
Property | Value |
---|---|
Rule ID | CA2259 |
Title | Ensure ThreadStatic is only used with static fields |
Category | Usage |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 8 | As warning |
Cause
The ThreadStaticAttribute attribute is applied to an instance field.
Rule description
ThreadStaticAttribute, which indicates that the value of a field is unique for each thread, only affects static
(Shared
in Visual Basic) fields. When applied to instance fields, the attribute has no impact on behavior.
How to fix a violation
To fix a violation, remove the ThreadStaticAttribute attribute from the field.
Example
The following code snippet shows a violation of CA2259:
class C
{
[ThreadStatic]
public int number = 404;
}
Class C
<ThreadStatic>
Public number As Integer = 404
End Class
When to suppress warnings
It's safe to suppress a warning from this rule. However, the [ThreadStatic]
attribute is a nop and isn't doing what you intended it to do.
See also
التعاون معنا على GitHub
يمكن العثور على مصدر هذا المحتوى على GitHub حيث يمكنك أيضاً إضافة مشاكل وطلبات سحب ومراجعتها. للحصول على معلومات إضافية، اطلع على دليل المساهم لدينا.
الملاحظات
https://aka.ms/ContentUserFeedback.
قريبًا: خلال عام 2024، سنتخلص تدريجيًا من GitHub Issues بوصفها آلية إرسال ملاحظات للمحتوى ونستبدلها بنظام ملاحظات جديد. لمزيد من المعلومات، راجعإرسال الملاحظات وعرضها المتعلقة بـ