Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- TestRef t = new TestRef();
- t.Something = "Foo";
- DoSomething(t);
- Console.WriteLine(t.Something);
- }
- static public void DoSomething(TestRef t)
- {
- t.Something = "Bar";
- }
- }
- public class TestRef
- {
- public string Something { get; set; }
- }
- TestRef t = new TestRef();
- t.Something = "Foo";
- DoSomething(ref t);
- void DoSomething(ref TestRef t)
- {
- t = new TestRef();
- t.Something = "Not just a changed t, but a completely different TestRef object";
- }
- int x = 1;
- Change(ref x);
- Debug.Assert(x == 5);
- WillNotChange(x);
- Debug.Assert(x == 5); // Note: x doesn't become 10
- void Change(ref int x)
- {
- x = 5;
- }
- void WillNotChange(int x)
- {
- x = 10;
- }
- public void Method1(object obj) {
- obj = new Object();
- }
- public void Method2(object obj) {
- obj = _privateObject;
- }
- using System;
- class Program
- {
- static void Main(string[] args)
- {
- TestRef t = new TestRef();
- t.Something = "Foo";
- DoSomething(t);
- Console.WriteLine(t.Something);
- }
- static public void DoSomething(TestRef t)
- {
- t = new TestRef();
- t.Something = "Bar";
- }
- }
- public class TestRef
- {
- private string s;
- public string Something
- {
- get {return s;}
- set { s = value; }
- }
- }
- static public void DoSomething(ref TestRef t)
- {
- t = new TestRef();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement