Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule S4004: Collection properties should be readonly #280

Closed
valhristov opened this issue May 5, 2017 · 1 comment
Closed

Rule S4004: Collection properties should be readonly #280

valhristov opened this issue May 5, 2017 · 1 comment
Assignees
Milestone

Comments

@valhristov
Copy link
Contributor

valhristov commented May 5, 2017

RSPEC-4004

A writable collection property can be replaced by a completely different collection. Making it readonly prevents that while still allowing individual members to be set. If you want to allow the replacement of the whole collection the recommended pattern is to implement a method to remove all the elements (e.g. System.Collections.ArrayList.Clear) and a method to populate the collection (e.g. System.Collections.ArrayList.AddRange).

@jmevel
Copy link

jmevel commented Nov 29, 2023

Hi, I'm coming here after getting an issue with Sonar rules.

The problem with this rule is that it goes in "conflict" with S3956.
We, therefore can't use AddRange because it is not available for IList.

Are we supposed to re-implement our own AddRange extension method or is there another simpler way?
I don't really want to reinvent the wheel...

Thanks for your answer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants