Created
January 28, 2013 11:30
-
-
Save N-Carter/4654791 to your computer and use it in GitHub Desktop.
Using the ref keyword with reference types
This file contains hidden or 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
class ReferenceRefTest | |
{ | |
class C | |
{ | |
public int i; | |
public C(int i) {this.i = i;} | |
} | |
static int Main() | |
{ | |
var c1 = new C(1); | |
var c1_original = c1; | |
var c2 = new C(2); | |
Change(ref c1); | |
Console.WriteLine("c1.i == {0}", c1.i); | |
Console.WriteLine("c1_original.i == {0}", c1_original.i); | |
Console.WriteLine("c2.i == {0}", c2.i); | |
return 0; | |
} | |
static void Change(ref C c) | |
{ | |
c = new C(3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment