471,317 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Confused about interfaces.....

Yello,

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?

eg:
private bird bird1; //Has an IAnimal interface, (no, it does not inherit
animal obj)
private cat cat1; //""
private dog dog1; //""

private IAnimal[] myPets; //(<--will that even work?, I know that obj will
work,
// but I dont want to un-box, and
re-box continuously.)

void main()
{

bird1 = new bird();
cat1 = new cat();
dog1 = new dog();

myPets = new IAnimal[3];
myPets[0] = bird1;
myPets[1] = cat1;
myPets[2] = dog1;

for(int x =0; x < 3; x++)
myPets[x].dispose(true);
}

//(Perhaps disposing my pets might not have been the best choice.
// No, realy, I do like my pets, and dont plan to dispose of them.)
Jan 19 '06 #1
3 956
TheMadHatter <Th**********@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 - <sk***@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
Jan 19 '06 #2
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.
"Jon Skeet [C# MVP]" wrote:
TheMadHatter <Th**********@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 - <sk***@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

Jan 19 '06 #3
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

Jan 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by baylor | last post: by
8 posts views Thread by Chris | last post: by
30 posts views Thread by Frank Rizzo | last post: by
3 posts views Thread by Tim Wallace | last post: by
8 posts views Thread by John | last post: by
22 posts views Thread by RSH | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.