论坛首页 编程语言技术论坛

Why does C# have both 'ref' and 'out'?

浏览 2652 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (3) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-14   最后修改:2008-12-14

ref out 两种参数传递方式都允许调用者更改传递来的参数的值,两者之间的不同很小,但很重要。两者最重要的不同就是他们所修饰的参数的指定值的方式不同。

 

带有out参数的方法在被调用之前不需要为out参数指定值,但是,在方法返回之前必须为out参数指定值。很绕口啊,看个例子吧:

class OutExample
{
      // Splits a string containing a first and last name separated
      // by a space into two distinct strings, one containing the first name and one containing the last name

      static void SplitName(string fullName, out string firstName, out string lastName)
      {
            // NOTE: firstName and lastName have not been assigned to yet.  Their values cannot be used.
            int spaceIndex = fullName.IndexOf(' ');
            firstName = fullName.Substring(0, spaceIndex).Trim();
            lastName = fullName.Substring(spaceIndex).Trim();
      }

      static void Main()
      {
            string fullName = "Yuan Sha";
            string firstName;
            string lastName;

            // NOTE: firstName and lastName have not been assigned yet.  Their values may not be used.
            SplitName(fullName, out firstName, out lastName);
            // NOTE: firstName and lastName have been assigned, because the out parameter passing mode guarantees it.

            System.Console.WriteLine("First Name '{0}'. Last Name '{1}'", firstName, lastName);
      }
}

 

可以把out参数看成是方法的另外的返回值。当一个方法要返回多个函数值时out是非常有用的。

 

我觉得ref更像引用,使用之前必须初始化。ref修饰的参数在方法内部修改后会反映在方法外部。因为他是引用传递:

 

class RefExample
{
      static object FindNext(object value, object[] data, ref int index)
      {
            // NOTE: index can be used here because it is a ref parameter
            while (index < data.Length)
            {
                  if (data[index].Equals(value))
                  {
                        return data[index];
                  }
                   index += 1;
            }
             return null;
      }

      static void Main()
      {
            object[] data = new object[] {1,2,3,4,2,3,4,5,3};

            int index = 0;
            // NOTE: must assign to index before passing it as a ref parameter
            while (FindNext(3, data, ref index) != null)
            {
                  // NOTE: that FindNext may have modified the value of index
                  System.Console.WriteLine("Found at index {0}", index);
                  index += 1;
            }

            System.Console.WriteLine("Done Find");
      }
}

 

 

   发表时间:2008-12-23  
没看懂。。。这两者区别在哪?
0 请登录后投票
   发表时间:2008-12-28  
out就是我保证out一个结果
ref就是我只是ref,不保证out
0 请登录后投票
   发表时间:2008-12-29  
带有out参数的方法在被调用之前不需要为out参数指定值,但是,在方法返回之前必须为out参数指定值。
0 请登录后投票
   发表时间:2009-03-03  
ref使用之前必须初始化,而 out 只需要定义,不用初始化,但在方法返回之前必须为out参数指定值
0 请登录后投票
   发表时间:2009-03-04   最后修改:2009-03-04
有什么难的?

ref的值在方法内部可能被使用,因此可能需要被赋值,也可以被修改,可以看成是"in out"

out只是用来返回值的,通常该方法需要返回多个值,比如buffer, buffersize, requiresize...
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics