Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effect of properties or get/set methods on object size

In terms of object size, how do properties instead Get/Set methods affect object size if the exposed properties do not represent a state but simply delegate its getter and setter calls to another entity?

For example, consider the following classes:

public class Person
{
   Address _address = new Address();

   public string AddressName
   {
      get{ return _address.Name; }
      set { _address.Name = value; }
   }

   public string GetAddressName(){ return _address.Name; }
   public void SetAddressName(string name){ _address.Name = name; }

}

public Address
{
    public string Name { get; set; }
}

I am guessing that when a new Person is created, the CLR will take into consideration the potential size of AddressName property when determining how much memory to allocate. However, if all I exposed was the Get/Set AddressName methods, there will be no additional memory allocated to cater for an AddressName property. So, to conserve memory footprint, it is better in this case to use Get/Set methods. However, this will not make a difference with the Name property of the Address class as state is being preserved. Is this assumption correct?

like image 812
Klaus Nji Avatar asked Feb 22 '13 13:02

Klaus Nji


People also ask

Why we use get and set with properties?

These access modifiers define how users of the class can access the property. The get and set accessors for the same property may have different access modifiers. For example, the get may be public to allow read-only access from outside the type, and the set may be private or protected .

What are get and set properties in C sharp?

A get property accessor is used to return the property value, and a set property accessor is used to assign a new value. In C# 9 and later, an init property accessor is used to assign a new value only during object construction. These accessors can have different access levels.

Why use get set instead of public?

The main difference between making a field public vs. exposing it through getters/setters is holding control over the property. If you make a field public, it means you provide direct access to the caller. Then, the caller can do anything with your field, either knowingly or unknowingly.

How properties are different from methods in C#?

Methods (C# doesn't have functions) are better for expressing things that either change the state, or which have an expectation of taking some time and not necessarily being reproducible. They don't tend to work in binding / serialization etc. Note that properties are actually just a special way of writing methods.


2 Answers

The size of an individual object is not affected by the number of properties, methods, events etc. It affects the size of the type metadata, and the JITted code - but not the per-object size. That's only affected by instance fields.

I am guessing that when a new Person is created, the CLR will take into consideration the potential size of AddressName property when determining how much memory to allocate.

No. It only looks at your fields. You have a single field (_address) so that plus the normal object overhead are all the memory required for a single instance.

like image 167
Jon Skeet Avatar answered Nov 14 '22 23:11

Jon Skeet


As Jon already answered, the size of an object instance is determined only by its instance fields. However, let me go into a little bit more detail.

An object instance is at the very least as big as the sum of the sizes of its fields. Depending on field alignment constraints and the way the CLR (Common Language Runtime) layouts the fields, some padding bytes may need to be taken into account. For reference types, an instance has two additional hidden fields: the sync block index and the type reference. Both are native integers. On the heap, instances have a minimum size of 12 bytes (regardless of how much space they actually need).

Take for example the following class:

class X
{
    byte value1;
    string value2;
    int value3;
    long value4;
}

On a 32-bit system, an instance of X may require 28 bytes:

  • 2 times four bytes for the sync block index and type reference,
  • one byte for byte,
  • three padding bytes, to align the next field,
  • four bytes for the object reference string,
  • another four bytes for int
  • and eight bytes for long,

As you can see, the getters and setters, methods and constructors do not take up space in each object instance. Instead, there is a separate object (which you know as Type) that maintains a list of methods (getters, setters and constructors are also methods) and contains the static fields of the type. You can create as many object instances of a type as you want, but there will only be one Type object for any given type instance.

like image 28
Daniel A.A. Pelsmaeker Avatar answered Nov 14 '22 23:11

Daniel A.A. Pelsmaeker