Created
March 15, 2012 06:47
-
-
Save hatelove/2042571 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reference type (because of 'class') | |
class SomeRef { public Int32 x; } | |
// Value type (because of 'struct') | |
struct SomeVal { public Int32 x; } | |
static void ValueTypeDemo() { | |
SomeRef r1 = new SomeRef(); // Allocated in heap | |
SomeVal v1 = new SomeVal(); // Allocated on stack | |
Chapter 5 Primitive, Reference, and Value Types 123 | |
r1.x = 5; // Pointer dereference | |
v1.x = 5; // Changed on stack | |
Console.WriteLine(r1.x); // Displays "5" | |
Console.WriteLine(v1.x); // Also displays "5" | |
// The left side of Figure 5-2 reflects the situation | |
// after the lines above have executed. | |
SomeRef r2 = r1; // Copies reference (pointer) only | |
SomeVal v2 = v1; // Allocate on stack & copies members | |
r1.x = 8; // Changes r1.x and r2.x | |
v1.x = 9; // Changes v1.x, not v2.x | |
Console.WriteLine(r1.x); // Displays "8" | |
Console.WriteLine(r2.x); // Displays "8" | |
Console.WriteLine(v1.x); // Displays "9" | |
Console.WriteLine(v2.x); // Displays "5" | |
// The right side of Figure 5-2 reflects the situation | |
// after ALL of the lines above have executed. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment