CLR optimization question

  • Thread starter Thread starter Daniel Billingsley
  • Start date Start date
D

Daniel Billingsley

Suppose you have a base class with one protected property and one method.

When you instantiate a derived class, obviously there needs to be a copy of
the property specific to that instantiation in memory.

But there *could* be one copy of the method for all the derived objects to
reference. Or does the compiler simply create a class with all the base
class code included and that's what the CLR creates at runtime?
 
Daniel,
When you instantiate a derived class, obviously there needs to be a copy of
the property specific to that instantiation in memory.

Not really. Each instance has its own copy of any instance fields that
you may use as a backing store for the property, but the property code
is shared by all instances.

But there *could* be one copy of the method for all the derived objects to
reference.

Yes, only a single copy of the code is needed.

Or does the compiler simply create a class with all the base
class code included and that's what the CLR creates at runtime?

No.



Mattias
 
Thanks for the response.
Not really. Each instance has its own copy of any instance fields that
you may use as a backing store for the property, but the property code
is shared by all instances.

Yeah, I really was thinking field but said property. Doh!

That's pretty cool.
 

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

Back
Top