C# — Is this checking necessary “ obj is Person && obj!= null”?

My preference would be to use the as keyword.

My preference would be to use the as keyword. Public override bool Equals(object obj) { var objectToCompare = obj as Person; if ( objectToCompare == null ) return false; ... } The advantage is that later in the method, you have a typed instance (objectToCompare) with which to do your comparisons. You are correct in your assessment that obj is Person will return false if obj is not derived from Person or if obj is null and thus obj is Person && obj!

= null is redundant; you only need obj is Person if you are using that style. Technically, there might be a fractional performance gain by checking for null first, but the gain would be negligible.

Depends on what the definition of is is ;-) In the case of . NET, as internally performs an is and sets the result to null if is is false. Jlew's solution is more optimal if null might sometimes be expected.

– Eric J. May 5 at 17:01 @Eric J :D. The reason for using as is primarily to have use of the typed instance later.So, in jlew's solution, you would (presumably) need to cast obj as a Person after you determined it wasn't null so that you could do the rest of the Equals evaluation.

Saves a line or two of code for the same result. – Thomas May 5 at 17:08.

Functionally it is correct, but it is quicker to check for null than to do a runtime type check, so you're better off checking for null first in most situations. That way, if obj is null, the overhead of the runtime type check will not be incurred.

Please see my updated post. – q0987 May 5 at 17:03 2 @q0987, Well, I just did a quick performance test and it turns out to be essentially the same performance either way. Either operation can be performed hundreds of millions of times per second, so it's probably not worth worrying about from the performance standpoint.

– jlew May 5 at 17:18.

Your version looks more correct to me. It won't be a Person unless it's non-null, so the obj! = null is redundant.

A reference to a Person can still be a null reference, so technically yes, both checks are required. I like Thomas' answer as to how to handle both checks at once. Null as MyClass == null, and myClassInstance as OtherClass == null, so with one check of the safely-cast object you've confirmed both conditions, and as he said, you have a strongly-typed reference to work with from then on.

There is an interesting discussion of the difference at low levels between is and as keyword operations. Google "Is as is or is is as" (I'm having Internet trouble at present). It turns out they work very similarly at the IL level.

1 +1 For getting me to look up "Is as is or is is as". Blogs.msdn. Com/b/ericlippert/archive/2010/09/16/… – Thomas May 5 at 17:21.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions