Does Properties increase memory size of Instances?

后端 未结 5 576
天涯浪人
天涯浪人 2021-01-19 10:30

This is probably a stupid question, but does object Properties occupy any memory per instance?

As I\'ve understand it when you instantiate an object each value field

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-19 11:18

    Properties are tranlated into two (or just one in case You provided only a getter or perhaps a setter) method that is

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

    is translated during compilation (probably Eric Lipper will correct me on this, becasue maybe it is during the preprocessing phase or sth) into methods

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

    all in all they carry no other overhead other than just to additional methods inluced in the object

提交回复
热议问题