Hum... what an intresting answer...
Say, if I deserialize an obj from a (eg) filestream, then cast,
are you saying that there isnt any real casting happening?,
thus there isnt any cost in preformance in doing the cast?
As for my previous example, I envisioned a heap obj, not
a value type.
Thanks for the previous answer.
[quoted text, click to view] "Jon Skeet [C# MVP]" wrote:
> TheMadHatter <TheMadHatter@discussions.microsoft.com> wrote:
> > Quick Q:
> > If I have a few objects and cast them(?) into an array of a piticular
> > interface,
> > would I have boxed the objects? Thus I wouldnt continuously re-box
> > that type, inturn saving processing time?
>
> Boxing only occurs if you're dealing with value types. You haven't
> shown the declarations of cat, bird or dog, so it's a bit hard to say
> what's happening. However, you're right that if they *are* value types,
> you'll only get the boxing once, when you put them in the array, and
> they won't be unboxed unless you cast them back to their original
> types.
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too
[quoted text, click to view] TheMadHatter wrote:
> Hum... what an intresting answer...
>
> Say, if I deserialize an obj from a (eg) filestream, then cast,
> are you saying that there isnt any real casting happening?,
> thus there isnt any cost in preformance in doing the cast?
>
> As for my previous example, I envisioned a heap obj, not
> a value type.
If the type involved is a reference type, and if there isn't any
user-defined conversion occurring, then the cast is really nothing more
than a check. Now, I would include that as "real casting" but that's a
matter of communication rather than behaviour.
There *is* a performance cost in doing a cast - the runtime has to
check that the type is an appropriate one. However, that cost is pretty
small.
Jon
[quoted text, click to view] TheMadHatter <TheMadHatter@discussions.microsoft.com> wrote:
> Quick Q:
> If I have a few objects and cast them(?) into an array of a piticular
> interface,
> would I have boxed the objects? Thus I wouldnt continuously re-box
> that type, inturn saving processing time?
Boxing only occurs if you're dealing with value types. You haven't
shown the declarations of cat, bird or dog, so it's a bit hard to say
what's happening. However, you're right that if they *are* value types,
you'll only get the boxing once, when you put them in the array, and
they won't be unboxed unless you cast them back to their original
types.
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet