You cannot, in principle. Not directly. Aliasing "pass by reference" is valid only in the method itself.
The closest you could get is a modified shell:
public class Wrapper<T> { public T Value { get; set; } public Wrapper(T value) { Value = value; } }
Then:
Wrapper<int> wrapper = new Wrapper<int>(1); ... TestClass tc = new TestClass(wrapper); Console.WriteLine(wrapper.Value); // 1 tc.ModifyWrapper(); Console.WriteLine(wrapper.Value); // 2 ... class TestClass { private readonly Wrapper<int> wrapper; public TestClass(Wrapper<int> wrapper) { this.wrapper = wrapper; } public void ModifyWrapper() { wrapper.Value = 2; } }
You can find Eric Lippert's latest blog post on "ref returns and ref locals" .
Jon skeet
source share