which versions are you using (SonarQube - Version 6.7 (build 33306) , SonartLint for VS 2019 4.3)
what are you trying to achieve
to fix s4275 rule
what have you tried so far to achieve this
am using a private filed in get and adding a value in loop through set but not able to fix the rule
need to know how to refactor it
Code Sample:
private readonly MyCollection _myPrivateField;
public MyCollection Data
{
get => _myPrivateField;
private set (//Am getting S4275 error here)
{
foreach (var item in value)
{
_myPrivateField.Add(item);
}
}
}
MyCollection.cs
public class MyCollection : KeyedCollection<string, MyModel>
MyModel.cs
public int ID {get;set;}
public string Name {get; set;}
Tried changing accessor from private to public, protected and removed readonly as well for private variable but not able to fix the error so far
Please suggest me an approach to handle this violation
Since its a bug sonar quality gate is failing
Awaiting for help
which is the version of the SonarCSharp analyzer you are using?
I highly recommend to update your SonarQube and SonarLint versions to the latest ones - for SQ, if you want stability you can choose SQ 7.9 LTS https://www.sonarqube.org/downloads/
private readonly List<int> _data = new List<int>();
public List<int> Data {
get => _data;
private set {
foreach (var item in value) {
_data.Add(value);
}
}
}
The setter in this case is only executed by a deserializer, but we don’t want to replace the field instance.