Skip to content

Instantly share code, notes, and snippets.

@hagbarddenstore
Created February 1, 2013 13:16
Show Gist options
  • Save hagbarddenstore/4691242 to your computer and use it in GitHub Desktop.
Save hagbarddenstore/4691242 to your computer and use it in GitHub Desktop.
void Main()
{
var a = new A(1);
var b = new B(2);
Console.WriteLine("Value of A: {0}", a.Value);
Console.WriteLine("Value of B: {0}", b.Value);
ChangeA(a);
Console.WriteLine("Value of A: {0}", a.Value);
ChangeARef(ref a);
Console.WriteLine("Value of A: {0}", a.Value);
ChangeAValue(a);
Console.WriteLine("Value of A: {0}", a.Value);
ChangeB(b);
Console.WriteLine("Value of B: {0}", b.Value);
ChangeBRef(ref b);
Console.WriteLine("Value of B: {0}", b.Value);
}
void ChangeA(A a)
{
Console.WriteLine("ChangeA");
a = new A(3);
}
void ChangeARef(ref A a)
{
Console.WriteLine("ChangeARef");
a = new A(4);
}
void ChangeAValue(A a)
{
Console.WriteLine("ChangeAValue");
a.Value = 5;
}
void ChangeB(B b)
{
Console.WriteLine("ChangeB");
b.Value = 6;
}
void ChangeBRef(ref B b)
{
Console.WriteLine("ChangeBRef");
b = new B(7);
}
class A
{
public A(int value)
{
Value = value;
}
public int Value { get; set; }
}
struct B
{
public int Value;
public B(int value)
{
Value = value;
}
}
@hagbarddenstore
Copy link
Author

Result when this program is run:

Value of A: 1
Value of B: 2
ChangeA
Value of A: 1
ChangeARef
Value of A: 4
ChangeAValue
Value of A: 5
ChangeB
Value of B: 2
ChangeBRef
Value of B: 7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment