对物体大小性能的影响或get / set方法 [英] Effect of properties or get/set methods on object size

查看:206
本文介绍了对物体大小性能的影响或get / set方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对象的规模而言,如何属性,而不是get / set方法影响对象的大小,如果公开的属性并不代表一个国家,而只是委托其getter和setter调用另一个实体?

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; }
}



我猜测,创建一个新的人的时候,CLR将采取考虑确定多少内存来分配,当AddressName财产的潜在规模。但是,如果所有我接触了get / set方法AddressName方法,有将分配给满足的AddressName财产没有额外的内存。因此,以节省内存占用,正是在这种情况下,最好使用get / set方法。但是,这将不会与Address类的名称属性的差异作为国家被保留。这是假设是正确的?

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?

推荐答案

的大小的单个对象的不受数量属性,方法,事件等,这影响了类型元数据的大小,和即时编译代码 - 但不是每个对象的大小。这是的只有的影响领域。

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 fields.

我猜测,创建一个新的人的时候,CLR将采取考虑确定多少内存来分配,当AddressName财产的潜在规模。

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.

没有。它的只有的看你的领域。你有一个单一的领域( _address ),这样加上正常的对象的开销都是单个实例所需的内存。

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.

这篇关于对物体大小性能的影响或get / set方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆