ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Code inspection: Collection content is never queried (private accessibility)

Last modified: 11 February 2024

ReSharper analyzes access to collections created in your code and warns you if a collection is updated but its contents are never used. For example:

All standard collection types are analyzed this way by default. If necessary, you can also apply this analysis to your custom collections that derive from IEnumerable. In this case, you need to tell ReSharper how invocations of collection’s public methods will affect the contents of the collection. To do so, mark the methods of your collection class with the [CollectionAccessAttribute], which should have one of four flags: None, Read, ModifyExisitingContent, UpdatedContent (the difference between the last two flags is that UpdatedContent means that the marked function can also add new elements to the collection). Here is a simple example to illustrate this:

Using JetBrains.Annotations to improve code analysis of collection access