您的位置:首页 > 产品设计 > UI/UE

Reference Types and Values Types

2005-01-20 10:13 267 查看
Reference Types and Values Types:[/b] 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
// v1’s fields have been initialized to 0.
SomeVal v1 = new SomeVal();
Int32 a = v1.x;
// These two lines don’t compile because C# doesn’t think that
// v1’s fields have been initialized to 0.
SomeVal v1;
Int32 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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: