Skip to content

Instantly share code, notes, and snippets.

@N-Carter
Created January 28, 2013 11:30
Show Gist options
  • Save N-Carter/4654791 to your computer and use it in GitHub Desktop.
Save N-Carter/4654791 to your computer and use it in GitHub Desktop.
Using the ref keyword with reference types
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