Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do method instructions add overhead to objects?

Status
Not open for further replies.

dragonwell

Programmer
Oct 21, 2002
863
US
My C# class has a few "longish" methods. I'm assuming that the instructions for these non-static methods are incorporated into every instance. So, I'm thinking that I could create a "service" class that has static methods which take instances of my class and provide the same code. The idea is that the classes use up less memory, since they get the bulk of their instruction code externally, rather than having it all inside itself. Is it worth it?

David
[pipe]
 
I don't know how C# is implemented, but most other OO languges have one set of methods in memory for each class, and only the actual data members are duplicated between classes. That means adding a method to a class shouldn't increase the size of an object of that class. I'd assume the same goes for C#, since it'd be horribly backwards and inefficient not to do it that way.

If the code for a method is always going to be, for example:

Code:
{
    x++;
    y += 40;
    reset_stuff();
    return z;
}

then there's no need for the compiler to create multiple copies of it, since the method call always results in that exact same code.


Methods are already essentially "static" functions that take the member as an implicit parameter. That's what they'd look like if you looked at them under the hood.


If C# has a sizeof operator, play around with that and you'll probably see that the size of your object is determined solely by the data memebers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top