Does memory increase the size of instances?

This is probably a silly question, but do Properties objects really occupy any memory per instance?

As I understand it, when you create an instance of an object, each value field takes its size, and the reference field takes 4 bytes per field. But tell me that you have an object with 1000 properties that retrieve data through some other object, do these properties have any memory on their own?

Automatic properties naturally do, since it is just syntactic sugar, but it does not look like ordinary properties ...

+4
source share
5 answers

Properties are similar to conventional methods in this regard.

The code should be stored somewhere (once for each type), and any fields used (Auto! Properties) should be stored on each instance. Local variables also take up some memory.

+8
source

Straight from Apress Illustrated C #

Unlike a field, however, a property is a function member. - It does not allocate memory for data storage! 
+4
source

No, properties are just syntactic sugar for the getter and setter methods. Occupy memory. If you do not have support fields, you will not have memory usage for each instance.

+1
source

If you look at the compiled C # class through, for example, reflector , you will see that the compiler actually translates the properties into get and set methods, automatic properties are converted to get and set methods using the support field, so the field will take up as much space as regular field

0
source

Properties are translated into two (or only one if you provided only the getter method or, possibly, the setter), which

 public int MyProp { get { return 1; } set { myField = value; } } 

translates at compile time (probably Eric Lipper will correct me, because maybe it is at the preprocessing stage or sth) into methods

 public int Get_MyProp(); public int Set_MyProp(int value); 

all of them do not carry other overhead costs, except for only additional methods used in the facility

0
source

All Articles