I noticed that Resharper suggests that I turn this:
if (myObj.myProp is MyType)
{
...
}
into this:
var myObjRef = myObj.
Resharper warning:
"Type check and direct cast can be replaced with try cast and check for null"
Both will work, it depends how your code suits you more. In my case I just ignore that warning:
//1st way is n+1 times of casting
if (x is A) ((A)x).Run();
else if (x is B) ((B)x).Run();
else if (x is C) ((C)x).Run();
else if (x is D) ((D)x).Run();
//...
else if (x is N) ((N)x).Run();
//...
else if (x is Z) ((Z)x).Run();
//2nd way is z times of casting
var a = x as Type A;
var b = x as Type B;
var c = x as Type C;
//..
var n = x as Type N;
//..
var z = x as Type Z;
if (a != null) a.Run();
elseif (b != null) b.Run();
elseif (c != null) c.Run();
...
elseif (n != null) n.Run();
...
elseif (x != null) x.Run();
In my code 2nd way is longer and worse performance.