C# -- Is this checking necessary " obj is Person && obj != null" <p>I saw the following code,</p> <pre><code>public override bool Equals(object obj) { // From the book http://www.amazon.co.uk/Pro-2010-NET-4-0-Platform/dp/1430225491 // Page 254! if (obj is Person &amp;&amp; obj != null) ... } </code></pre> <p>Based on my understanding, I think the code should be rewritten as follows:</p> <pre><code>public override bool Equals(object obj) { if (obj is Person) ... } </code></pre> <p>Is that correct?</p> <p>Based on <a href="http://msdn.microsoft.com/en-us/library/scekt9xw%28v=vs.80%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/scekt9xw%28v=vs.80%29.aspx</a></p> <p><em>An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown.</em> </p> <p>I think the extra checking for null is NOT necessary at all. In other words, that code "obj != null" should never be hit at all.</p> <p>Thank you</p> <p>// Updated //</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Employee { public static void CheckIsEmployee(object obj) { if (obj is Employee) { Console.WriteLine("this is an employee"); } else if (obj == null) { Console.WriteLine("this is null"); } else { Console.WriteLine("this is Not an employee"); } } } class NotEmployee { } class Program { static void Main(string[] args) { Employee e = new Employee(); Employee.CheckIsEmployee(e); Employee f = null; Employee.CheckIsEmployee(f); NotEmployee g = new NotEmployee(); Employee.CheckIsEmployee(g); } } } </code></pre> <p>Output results:</p> <pre><code>this is an employee this is null this is Not an employee </code></pre>