Reference Types and Values ​​Types

xiaoxiao2021-03-06  39

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 Consideration When You're Working with Reference Types:

The Memory Must Be Allocated from The Manage HEAP.

Each Object Allocated on The Heap Has Some Additional Overhead Members Associated with it this 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 'THE 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 TWRESENTATIONS: 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.

转载请注明原文地址:https://www.9cbs.com/read-66761.html

New Post(0)