C# ref keyword usage

后端 未结 7 1358
耶瑟儿~
耶瑟儿~ 2020-12-10 03:39

I understand (or at least I believe I do) what it means to pass an instance of a class to a method by ref versus not passing by ref. When or under

相关标签:
7条回答
  • 2020-12-10 04:39

    Put succinctly, you would pass a value as a ref parameter if you want the function you're calling to be able to alter the value of that variable.

    This is not the same as passing a reference type as a parameter to a function. In those cases, you're still passing by value, but the value is a reference. In the case of passing by ref, then an actual reference to the variable is sent; essentially, you and the function you're calling "share" the same variable.

    Consider the following:

    public void Foo(ref int bar)
    {
        bar = 5;
    }
    
    ...
    
    int baz = 2;
    
    Foo(ref baz);
    

    In this case, the baz variable has a value of 5, since it was passed by reference. The semantics are entirely clear for value types, but not as clear for reference types.

    public class MyClass
    {
        public int PropName { get; set; }
    }
    
    public void Foo(MyClass bar)
    {
        bar.PropName = 5;
    }
    
    ...
    
    MyClass baz = new MyClass();
    
    baz.PropName = 2;
    
    Foo(baz);
    

    As expected, baz.PropName will be 5, since MyClass is a reference type. But let's do this:

    public void Foo(MyClass bar)
    {
        bar = new MyClass();
    
        bar.PropName = 5;
    }
    

    With the same calling code, baz.PropName will remain 2. This is because even though MyClass is a reference type, Foo has its own variable for bar; bar and baz just start out with the same value, but once Foo assigns a new value, they are just two different variables. If, however, we do this:

    public void Foo(ref MyClass bar)
    {
        bar = new MyClass();
    
        bar.PropName = 5;
    }
    
    ...
    
    MyClass baz = new MyClass();
    
    baz.PropName = 2;
    
    Foo(ref baz);
    

    We'll end up with PropName being 5, since we passed baz by reference, making the two functions "share" the same variable.

    0 讨论(0)
提交回复
热议问题