通过ref和out传递

时间:2009-10-29 15:05:23

标签: c# pass-by-reference

因此,如果我使用foreach循环进行迭代,并且我在内部有一个函数,它接受从列表迭代的对象的参数,并且假设我将其值设置为不同。 我为什么不用出来或参考?我认为它只是通过值传递,如果你没有使用或参考....我知道你必须在前后初始化变量你必须在从方法返回之前设置它的值。

看起来如果你通过列表​​迭代并传递一个实际通过引用传递的对象。请考虑以下示例。

实施例

class Program
    {
        static void Main(string[] args)
        {

            List<Foo> list = new List<Foo>();
            list.Add(new Foo() { Bar = "1" });
            list.Add(new Foo() { Bar = "2" });



            foreach (var f in list)
            {
                Foo f2 = f; 
                Console.WriteLine("SetFoo Pre: " + f2.Bar);
                SetFoo(f2);
                Console.WriteLine("SetFoo Post: " + f2.Bar);

                Console.WriteLine("SetFooRef Pre: " + f2.Bar);
                SetFooRef(ref f2);
                Console.WriteLine("SetFooRef Post: " + f2.Bar);
                Console.WriteLine("");
            }




            Console.WriteLine("");

            int i = 0;
            // Not using ref keyword
            Console.WriteLine("SetI Pre: " + i);
            SetI(i);
            Console.WriteLine("SetI Post: " + i);

            // Using ref keyword
            Console.WriteLine("SetRefI Pre: " + i);
            SetRefI(ref i);
            Console.WriteLine("SetRefI Post: " + i);
        }


        private static void SetRefI(ref int i)
        {
            i = 3;
            Console.WriteLine("SetRefI Inside: " + i);
        }

        private static void SetI(int i)
        {
            i = 2;
            Console.WriteLine("SetI Inside: " + i);
        }

        private static void SetFooRef(ref Foo f)
        {
            f.Bar = String.Format("{0} :: {1}", f.Bar, "WithRef");
            Console.WriteLine("SetFooRef Inside: " + f.Bar);
        }

        private static void SetFoo(Foo f)
        {
            f.Bar = String.Format("{0} :: {1}", f.Bar, "WithoutRef");
            Console.WriteLine("SetFoo Inside: " + f.Bar);
        }
    }


    class Foo
    {
        public string Bar { get; set; }
    }

输出:

  

SetFoo Pre:1 SetFoo Inside:1 ::
  WithoutRef SetFoo Post:1 WithoutRef
  SetFoo Pre:1 :: WithoutRef SetFoo
  内部:1 :: WithoutRef :: WithRef
  SetFoo Post:1 WithoutRef :: WithRef

     

SetFoo Pre:2 SetFoo Inside:2 ::
  WithoutRef SetFoo Post:2 WithoutRef
  SetFoo Pre:2 :: WithoutRef SetFoo
  内部:2 :: WithoutRef :: WithRef
  SetFoo Post:2 WithoutRef :: WithRef

     

SetI Pre:0 SetI Inside:2 SetIPost:0

     

SetRefI Pre:0 SetRefI Inside:3
  SetRefI Post:3

我理解带有整数示例的ref,但不理解上面的Foo对象迭代示例。

谢谢!

1 个答案:

答案 0 :(得分:12)

引用按值传递。因此该方法仍然可以更改对象的内容,它只是无法更改变量引用的对象。

有关更多信息,请参阅my article on parameter passing以及有关reference types and value types的文章。

相关问题