【问题标题】:What is the difference between 2 methods with ref object par and without?有 ref object par 和没有 ref object par 的 2 种方法有什么区别?
【发布时间】:2013-05-04 21:54:37
【问题描述】:

我想知道以下方法在如何引用对象参数方面有什么区别:

public void DoSomething(object parameter){}

和

public void DoSomething(ref object parameter){}

如果我想更改对object 的引用而不是覆盖同一引用中的对象,是否应该使用ref object parameter?

【问题讨论】:

标签: c# ref


【解决方案1】:
public void DoSomething(object parameter)
{
  parameter = new Object(); // original object from the callee would be unaffected. 
}

public void DoSomething(ref object parameter)
{
  parameter = new Object(); // original object would be a new object 
}

见文章:Parameter Passing in C# by Jon Skeet

在 C# 中,引用类型对象的地址是按值传递的,当使用 ref 关键字时,可以为原始对象分配一个新对象或 null,而没有 ref 关键字是不可能的。

考虑以下示例:

class Program
{
    static void Main(string[] args)
    {
        Object obj1 = new object();
        obj1 = "Something";

        DoSomething(obj1);
        Console.WriteLine(obj1);

        DoSomethingCreateNew(ref obj1);
        Console.WriteLine(obj1);

        DoSomethingAssignNull(ref obj1);
        Console.WriteLine(obj1 == null);
        Console.ReadLine();
    }
    public static void DoSomething(object parameter)
    {
        parameter = new Object(); // original object from the callee would be unaffected. 
    }

    public static void DoSomethingCreateNew(ref object parameter)
    {
        parameter = new Object(); // original object would be a new object 
    }

    public static void DoSomethingAssignNull(ref object parameter)
    {
        parameter = null; // original object would be a null 
    }
}

输出将是:

Something
System.Object
True

【讨论】:

    【解决方案2】:

    参数传递 ByVal: 描述按值传递参数,这意味着过程不能修改变量本身。

    参数传递 ByRef: 描述通过引用传递参数,这意味着过程可以修改变量本身。

    【讨论】:

    • 但是objects默认通过引用传入c#。
    • 除非您将引用本身视为对象的值?
    【解决方案3】:

    当您看到 ref object 时,这意味着参数必须是 object 类型。

    您可以阅读文档:

    当形参是引用形参时,对应的 方法调用中的参数必须由关键字 ref 组成 后跟一个与 形式参数。一个变量必须在它可以被明确分配之前 作为参考参数传递。

    【讨论】:

      【解决方案4】:

      通过 ref 传递变量允许函数将该变量重新指向另一个对象,或者确实为 null:例如

      object parameter = new object();
      
      FailedChangeRef(parameter); // parameter still points to the same object
      ChangeRef(ref parameter); // parameter now points to null
      
      public void FailedChangeRef(object parameter)
      {
                  parameter = null; // this has no effect on the calling variable
      }
      
      public void ChangeRef(ref object parameter)
      {
                  parameter = null;  
      }
      

      【讨论】:

        【解决方案5】:

        在 C# 中,方法参数的默认机制是按值传递。因此,如果您要声明类似的方法,

        public void DoSomething(object parameter){} // Pass by value
        

        所以,创建了一个新的对象副本,因此对参数的更改不会影响传入的原始对象。

        但是,当你通过ref传递参数时,它是通过引用传递

        public void DoSomething(ref object parameter) // Pass by reference
        

        现在,您正在对最初传递的对象的地址进行操作。因此,您对方法内部参数所做的更改将影响原始对象。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-23
          • 1970-01-01
          • 1970-01-01
          • 2011-08-12
          • 2013-05-31
          相关资源
          最近更新 更多