Background:
I was writing some code. It looked something like this:
class X
{
private List<int> _myList = new List<int>();
public void MyMethod(int x)
{
_myList.Add(x);
}
}
R# suggested that I make _myList
readonly. I wasn't so sure, as though I technically could make it readonly, I think it would be slightly confusing to do so, as I'm still modifying the list, just not reassigning it. So while I could make it readonly, I'm not sure if I should.
Question:
Should I make variables like this readonly? Why? If not, why does R# suggest this?
"I'm still modifying the list"
), so he's thinking that havingreadonly
there would be confusing. Apparently it has confused you, so that would be a point in the "probably shouldn't add readonly" column.