Reference Types and Values Types

Reference Types and Values Types: reference types and value types. Of the two, you will run into reference types much more often. Reference types are always allocated from the managed heap, and the c # operator returns the memory address of the object- the memory address refers to the object’s bits. You need to bear in mind some performance considerations when you’re working with reference types. First consider these facts:

   The memory must be allocated from the manage heap.

   Each object allocated on the heap has some additional overhead members associated with it that must be initialized.

 

Allocating an object from the manage heap could force a garbage collection to occur

// These two lines compile because C# thinks that

// v 1’ s fields have been initialized to 0.

SomeVal v1 = new SomeVal();

Int 32 a = v1.x;

// These two lines don’t compile because C# doesn’t think that

// v 1’ s fields have been initialized to 0.

SomeVal v1;

Int 32 a = v1.x; // error CS0170: Use of possibly unassigned field

The main advantage of value types is that they’re no allocated in the managed heap. Of course, value types have several limitations of their own when compared to reference types. Here are some of the ways in which value types and reference types differ:

  .Value type objects have two representations: an unboxed form and a boxed form. Reference types are always in a boxed form.

   Value types are derived from System.ValueType. This type offers the same methods as defined by System.Object. However, System.ValueType overrides the Equals method so that is return true if the values of the two object’s fields match.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值