Please follow this template to help us specify this new rule:
-
description of the Rule. It should answer questions “why is there an issue?”,
Generic dictionaries support multiple concurrent readers as long as the collection is not modified. However, enumerating through the collection is not a thread-safe operation. In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization. Ensure you implement synchronization if you access a generic collection concurrently. -
snippet of Noncompliant Code
-
snippet of Compilant Code (fixing the above noncompliant code)
-
exceptions to the Noncompliant Code, i.e. conditions in which the noncompliant code should not raise an issue so that we reduce the number of False Positives.
-
external references and/or language specifications
Generic Dictionary documentation on MSDN: Dictionary<TKey,TValue> Class (System.Collections.Generic) | Microsoft Learn
• Microsoft Blog article, ‘High CPU in .NET app using a static Generic.Dictionary’: High CPU in .NET app using a static Generic.Dictionary - If broken it is, fix it you should -
type : Bug
Guidelines:
We want to add as many valuable rules as possible. Thus we have guidelines to help us see the value of a rule and decide if it should be implemented. Please read them before submitting your rule:
- Is the rule useful for a developer.
- If the rule is a Bug, Code Smell or Vulnerability it should ask the developer to fix a real problem. It shouldn’t raise warnings asking for a manual review.