R
Rob S
I'm new to C#, new to a project, and wondering about the differences
between two ways of handling a particular situation in our code. In a
nutshell, what is the performance difference between "is" and "as"
operations?
Basically, we have an event that is triggered each time a sub object is
added to a parent object - call it SuperWidget. By its nature, single
instances of some types of subobjects are added (SuperWidgetHeader,
SuperWidgetFooter, etc.), and multiple instances of another type of
object are added (SuperWidgetWidget). The number of SuperWidgetWidgets
may vary from say 4 to 45, so this event gets triggered from less than
10 to almost 50 times, depending upon the particular instance of
SuperWidget. (There may end up being dozens so SuperWidgets created
and destroyed throughout the day.)
We need to do some special handling for the SuperWidgetHeader, so in
the event handler we're using something like:
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
if (null != playObject) then
{
playObject.SomeMethod()
...
if (sourceObject is SuperWidgetHeader)
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...
but I'm told the first version is more efficient because the "as"
casting is less expensive than the "is" casting (not to mention in the
second version once we find the right type of object, we have to cast
it a second time). They can't point me to a reference to this though,
and I haven't been able to dig up anything, either.
Functionally, what is the difference between the two, and is the
overhead worth sacrificing for sake of readability?
For what its worth, research suggests a more efficient way to handle
this may just be something like:
if (sourceObject.ToString() == "SuperWidgetHeader")
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...
because (depending upon how SuperWidget impliments it) ToString() may
avoid the expensive casting issues altogether, but we all seem to agree
embedding strings in the code for comparison purposes runs counter to
the whole object philosophy, so we're pretty much rejecting this
approach completely. How invalid is this thinking?
At any rate, back to the original question - what's the difference
between "is" and "as"?
between two ways of handling a particular situation in our code. In a
nutshell, what is the performance difference between "is" and "as"
operations?
Basically, we have an event that is triggered each time a sub object is
added to a parent object - call it SuperWidget. By its nature, single
instances of some types of subobjects are added (SuperWidgetHeader,
SuperWidgetFooter, etc.), and multiple instances of another type of
object are added (SuperWidgetWidget). The number of SuperWidgetWidgets
may vary from say 4 to 45, so this event gets triggered from less than
10 to almost 50 times, depending upon the particular instance of
SuperWidget. (There may end up being dozens so SuperWidgets created
and destroyed throughout the day.)
We need to do some special handling for the SuperWidgetHeader, so in
the event handler we're using something like:
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
if (null != playObject) then
{
playObject.SomeMethod()
...
From a readability standpoint, I think this is preferable:
if (sourceObject is SuperWidgetHeader)
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...
but I'm told the first version is more efficient because the "as"
casting is less expensive than the "is" casting (not to mention in the
second version once we find the right type of object, we have to cast
it a second time). They can't point me to a reference to this though,
and I haven't been able to dig up anything, either.
Functionally, what is the difference between the two, and is the
overhead worth sacrificing for sake of readability?
For what its worth, research suggests a more efficient way to handle
this may just be something like:
if (sourceObject.ToString() == "SuperWidgetHeader")
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...
because (depending upon how SuperWidget impliments it) ToString() may
avoid the expensive casting issues altogether, but we all seem to agree
embedding strings in the code for comparison purposes runs counter to
the whole object philosophy, so we're pretty much rejecting this
approach completely. How invalid is this thinking?
At any rate, back to the original question - what's the difference
between "is" and "as"?
