In C# you have two ways of casting one object to another.
One is to use the
as operator, which attempts to
casting the object and either returns the successfully cast result
Another way is to use the casting operator.
In the case where you are going to use the result of the cast immediately, Which should you use?
// option 1 var thing = GetCurrentItem(); var foo = thing as Foo; foo.DoSomething(); // option 2 var thing = GetCurrentItem(); var foo = (Foo)thing; foo.DoSomething();
Now, suppose the
thing is not a
Both options will crash, but they will crash differently.
In the first version, you will crash with a
and the crash dump will confirm that, yes, the
foo variable is null.
thing might not be in the crash dump.
Maybe the crash dump captures only the parameters that participated
in the expression that resulted in the exception.
thing was GC'd away.
You can't tell whether the problem is that
GetCurrentItem returned an object
that wasn't a
And if it wasn't a
Foo, what was it?
In the second version,
there are two ways the code could crash.
thing is null, then you will get a
But if the
is the wrong kind of object,
the crash will occur at the point of the cast
And if you're lucky, the debugger will show you the
thing that could not be cast.
Even if it doesn't, you can at least determine
from the type of the exception which of the two cases you're in.
Exercise: The following two lines of code are functionally equivalent. Which is easier to debug?
// option 1 collection.FirstOrDefault().DoSomething(); // option 2 collection.First().DoSomething();