Why use the 'ref' keyword when passing an object?

后端 未结 11 1531
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 06:45

If I am passing an object to a method, why should I use the ref keyword? Isn\'t this the default behaviour anyway?

For example:

class Program
{
    s         


        
相关标签:
11条回答
  • 2020-11-22 07:41

    This is like passing a pointer to a pointer in C. In .NET this will allow you to change what the original T refers to, personally though I think if you are doing that in .NET you have probably got a design issue!

    0 讨论(0)
  • 2020-11-22 07:41

    If you're passing a value, however, things are different. You can force a value to be passed by reference. This allows you to pass an integer to a method, for example, and have the method modify the integer on your behalf.

    0 讨论(0)
  • 2020-11-22 07:43

    In .NET when you pass any parameter to a method, a copy is created. In value types means that any modification you make to the value is at the method scope, and is lost when you exit the method.

    When passing a Reference Type, a copy is also made, but it is a copy of a reference, i.e. now you have TWO references in memory to the same object. So, if you use the reference to modify the object, it gets modified. But if you modify the reference itself - we must remember it is a copy - then any changes are also lost upon exiting the method.

    As people have said before, an assignment is a modification of the reference, thus is lost:

    public void Method1(object obj) {   
     obj = new Object(); 
    }
    
    public void Method2(object obj) {  
     obj = _privateObject; 
    }
    

    The methods above does not modifies the original object.

    A little modification of your example

     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; }
            }
        }
    
    0 讨论(0)
  • 2020-11-22 07:48

    With ref you can write:

    static public void DoSomething(ref TestRef t)
    {
        t = new TestRef();
    }
    

    And t will be changed after the method has completed.

    0 讨论(0)
  • 2020-11-22 07:50

    By using the ref keyword with reference types you are effectively passing a reference to the reference. In many ways it's the same as using the out keyword but with the minor difference that there's no guarantee that the method will actually assign anything to the ref'ed parameter.

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