Main Topics
Browse All TopicsI have two class like then:
class A
{
int i;
public A(){}
public void f() {...}
}
class B
{
int i;
public A(){}
public void f1() {...}
public void f2() {...}
public void f3() {...}
....
}
And i create 2 instance of two class.
A a = new A();
B b = new B();
Which is a bigger size in memory? And how to test this?
Thanks a lot.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I see... as far as I know people use inheritance for good programming practice and maintainability/reusabilit
As for the size and/or performance, I think it would depend on how you implement it.
eg. If you have different classes having same methods
vs if you make a new class containing the common methods, and have the classes inherit from it.
The latter would probably reduce the total size.
This is a very good topic for discussion
As far as, in calculating the size of teh object of a class,
it needs to consider folloiwng points
1) No of parameters and their datatypes
2) Any virtual method
3) Any static variable in class
So the size of teh object totally depends upon the no of parameters and their data types
If you have any virtual method., virtual table is used to store pointers
2 pradipbele:
>> 1) No of parameters and their datatypes
Do you mean member variables? Yes, they are the primary source for object size calculation.
>> 2) Any virtual method
Pointers to these are stored in the vtable, so they don't really affect the size of the object because there's at most one vtable per class, not per object. In C++, no vtable is created for a class with no virtual functions, but in C# I assume it's created anyway to support reflection.
>> 3) Any static variable in class
These do not affect the size of class objects at all, because they're shared by objects of the same class.
Implementing Hardi's reference, types A, C, D, and E below are all of length 119. Type B is longer, at 150, as one would expect.
using System;
using System.Runtime.Serializati
[Serializable] class A {
public int i;
}
[Serializable] class B {
public A anA = new A();
}
[Serializable] class C {
public int i;
public void f() { }
}
[Serializable] class D {
public int i;
public virtual void f() { }
}
[Serializable] class E : D {
public new void f() { base.f(); }
}
class Demo {
static void Main(string[] args) {
Console.WriteLine("A size = {0}",ObjectSize(new A()));
Console.WriteLine("B size = {0}",ObjectSize(new B()));
Console.WriteLine("C size = {0}",ObjectSize(new C()));
Console.WriteLine("D size = {0}",ObjectSize(new D()));
Console.WriteLine("E size = {0}",ObjectSize(new E()));
}
static long ObjectSize(object o) {
System.IO.MemoryStream stream = new System.IO.MemoryStream();
BinaryFormatter objFormatter = new BinaryFormatter();
objFormatter.Serialize(str
return stream.Length;
}
}
The size of a class instance is not affeted by the number of methods in the class definition.
I thought I'd try and prove that using the CLR Profiler (http://www.microsoft.com/
Running the attached code creates 10, 000 instances of the two classes in the original question, so any difference in instance size should be obvious to see in the profiler results.
Business Accounts
Answer for Membership
by: HardiPosted on 2008-04-08 at 21:12:15ID: 21311740
For C# 2.0 (that I use)...
As you may already know, typeof(type) cannot be used to even determine the type of string.
Because two strings of different length can have different sizes.
I think that's also the case with classes... so I don't think you can determine the size of class in C#.