1

I have two classes, one has a lot of properties and the other has a couple. I need to reference a few of the properties of the larger class in the smaller class, but I'm wondering if it would be better to just pass the values down individually. Example:

public class LargeClass
{
    private string _var1;
    private string _var2;
    private string _var3;
    //...
    private string _var20;
    //stuff
}

public class SmallClass
{
    private LargeClass _largeclass;
    private string _var1;

    public SmallClass(LargeClass LargeClass)
    {
        _largeclass = LargeClass;
        _var1 = _largeclass.Var1 + _largeclass.Var2;
    }
}

Or, I could do this for the small class and pass the values I need in directly:

public class SmallClass2
{
    private string _var1;
    private string _var2;
    private string _var3;

    public SmallClass2(string Var1, string Var2)
    {
        _var1 = Var1;
        _var2 = Var2;
        _var3 = _var1 + _var2;
    }
}

Basically, my question is which one of these uses up less space (both while running and if serialized)?

Update: I was able to rewrite my classes so that the smaller class objects referenced their parent objects and found that it definitely did use less space when serialized. Obviously, this result is case by case, but for my code I basically changed it from each instance of the small class storing a file path as a string to having the class capable of creating the path string on the fly using references to the parent objects.

The resulting serialized data file was 55% smaller.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lailoken
  • 95
  • 2
  • 5
  • 1
    What you _probably_ should consider here is if your `SmallClass` should know about `LargeClass` or not and how are those two related. Cases when you need to worry about such minor memory/performance differences are really rare. – doblak Feb 26 '12 at 00:57

3 Answers3

2

While running they will use the same amount of space since they are just reference variables (all point to the same memory location). If you are serializing the data - then you will see more overhead by serializing the larger class.

tsells
  • 2,751
  • 1
  • 18
  • 20
1

They will use nearly the same memory. Passing by reference creates a pointer to the object so no extra memory is used except the heap space required for the pointer. Regarding serialization, option B might take a bit less space if you use the standard binary serialization.

Bruno Silva
  • 3,077
  • 18
  • 20
1

I would not be worrying about serialization space in this case (except if you have to serialize thousands of instances).

I'd be more focus on type usage and maintenance. For example if you want the smaller class to contain the values of the properties as they were at the time the instance was created then I'll copy the values. Otherwise, I'd use a reference to the larger type. This way the smaller type will get all changes on the fields. I'd also replace the "_var1 + _var2" with a property unless "_var1" and "_var2" never change.

Coder
  • 376
  • 2
  • 4
  • Serialization was my main concern. I am serializing thousands of instances. The "_var1 + _var2" was just an example. My actual code is a bit more complex. – Lailoken Feb 26 '12 at 02:25
  • 1
    Binary serialization handles references so I'd definitely use a reference to the larger object. To be sure you can just try both cases by serializing 100 objects to a **MemoryStream** and check the size of the resulting stream. – Coder Feb 26 '12 at 08:38
  • See more about binary serialization here: http://stackoverflow.com/questions/4905768/net-binary-serialize-object-with-references-to-other-objects-what-happens – Coder Feb 26 '12 at 08:40