Changing field of a mutable type to read only
Not sure if there's a JIRA for this, but given this code:
private int[] foo = new int[] {1, 2, 3};
public int[] Foo { get { return foo; } }
public void ChangeFoo()
{
Foo[2] = 5;
}
Resharper suggests making foo read-only, but this is bad practice since foo
is of a mutable type: http://msdn2.microsoft.com/en-us/library/ms229057.aspx
Please sign in to leave a comment.
I do not see why this is a bad practice.
You shouldn't be confused by object immutability and immutability of pointer
to it.
C# has no ways to declare object as immutable
--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Dag Christensen" <anonymous@noreply.com> wrote in message
news:fjrc6t$5ci$1@is.intellij.net...
>
>
>
>
>
>
I definitely know the difference, and I'd probably have no problems
accepting "private readonly List]]> foo;" ... but I still think adding
readonly to the int[] in my example is bad coding practice for readability.
Probably no way for R# to see what's reasonable to make readonly or not
though, so forget it - no biggie (dang, that means I have to start thinking
before accepting quick-fixes ;)
"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:fjrgt1$imc$1@is.intellij.net...
>I do not see why this is a bad practice.
>
>
>> Not sure if there's a JIRA for this, but given this code:
>>
>> private int[] foo = new int[] {1, 2, 3};
>> public int[] Foo { get { return foo; } }
>>
>> public void ChangeFoo()
>> {
>> Foo[2] = 5;
>> }
>>
>>
>> Resharper suggests making foo read-only, but this is bad practice since
>> foo is of a mutable type:
>> http://msdn2.microsoft.com/en-us/library/ms229057.aspx
>>
>>
>>
>