-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathNullArgumentToEquals.qhelp
34 lines (29 loc) · 1.16 KB
/
NullArgumentToEquals.qhelp
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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>It is common to want to check an object against <code>null</code>, but this should not be done
using the <code>Equals</code> method. If the object really is <code>null</code>, a <code>
NullReferenceException</code> is thrown when attempting to call <code>Equals</code>, with
unexpected results.</p>
</overview>
<recommendation>
<p>The offending call should be replaced with either <code>==</code> or <code>ReferenceEquals</code>
(the difference being that <code>==</code> can be overridden but <code>ReferenceEquals</code>
cannot).</p>
</recommendation>
<example>
<p>In the following example, <code>IsNull</code> will throw a <code>NullReferenceException</code>
when <code>o</code> is <code>null</code>.
</p>
<sample src="NullArgumentToEqualsBad.cs" />
<p>In the revised example, <code>IsNull</code> will correctly return <code>true</code>
when <code>o</code> is <code>null</code>.
</p>
<sample src="NullArgumentToEqualsGood.cs" />
</example>
<references>
<li>MSDN: <a href="https://msdn.microsoft.com/en-us/library/bsc2ak47.aspx">Object.Equals Method (Object)</a>.</li>
</references>
</qhelp>