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.
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.
相关文章推荐
- CLR Via C# 3rd 阅读摘要 -- Chapter 5 - Primitive, Reference, and Value Types
- .net中6个重要的基础概念:Stack, heap, Value types, reference types, boxing and Unboxing.
- Effective C# Item 6: Distinguish Between Value Types and Reference Types
- CLR via C# 总结之Chap5 Primitive, Reference, and Value Types
- Item 6: Distinguish Between Value Types and Reference Types
- 6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing.
- Oracle Database PL/SQL Packages and Types Reference
- .Net的六个重要概念:Stack, heap, value types, reference types, boxing, and unboxing
- 6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing.
- Database PL/SQL Packages and Types Reference 168 UTL_FILE
- Type Fundamentals (.NET: Primitive types, reference types and value types )
- Primitive Types and Reference Types in Javascript
- 4.1 primitive and reference values
- oracle帮助文档——PL/SQL Packages and Types Reference
- Java 数据类型 - 整型(4.2.1. Integral Types and Values)
- Tuple Types and Multiple Return Values
- 6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing.
- Item 18: Distinguish Between Value Types and Reference Types(Effective C#)
- CLR.via.Csharp.3rd Chapter 5: Primitive, Reference, and Value Types
- 6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing