-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathMissedReadonlyOpportunity.qhelp
More file actions
39 lines (32 loc) · 1.01 KB
/
MissedReadonlyOpportunity.qhelp
File metadata and controls
39 lines (32 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
A private field where all assignments occur as part of the declaration or in a constructor
in the same class can be <code>readonly</code>. Making a field <code>readonly</code>
prevents unintended assignments after object initialization.
</p>
</overview>
<recommendation>
<p>
Add a <code>readonly</code> modifier to the field, unless changes to the field are
allowed after object initialization.
</p>
</recommendation>
<example>
<p>
In the following example, the field <code>Field</code> is only assigned to in the
constructor, but it can still be modified after object initialization.
</p>
<sample src="MissedReadonlyOpportunityBad.cs" />
<p>
In the revised example, the field is made <code>readonly</code>.
</p>
<sample src="MissedReadonlyOpportunityGood.cs" />
</example>
<references>
<li>Microsoft: <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly">readonly</a>.</li>
</references>
</qhelp>