Confused about interfaces.....


G

Guest

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.)
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

TheMadHatter said:
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.
 
G

Guest

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.
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

TheMadHatter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

about interface 4
About Interface 5
About Interface 2
Confused about reflection 8
Confused about generics ... 1
Confused about casting. 3
Confused about conversion 3
Exists. Confused about this ... 3

Top