路过的ref和out [英] Passing By ref and out

查看:148
本文介绍了路过的ref和out的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,如果我使用foreach循环迭代,我里面有一个函数接受从列表迭代对象的参数,可以说,我将其值设置有所不同。
为什么不我必须使用out或ref?我以为只是按值传递,如果你没有使用out或ref ....我知道,你必须初始化变量你就必须前后出裁判都从该方法返回之前设置其值。

So if I am iterating using a foreach loop and I have a function inside that takes an argument of the object iterated from list, and lets say i set its value to be different. Why don't I have to use out or ref ? I thought it was only passed by value if it you didn't use out or ref.... I know that a ref you must have initialized the variable before and out you just have to have set its value before return from the method.

好像如果一个迭代直通名单,并通过其实际按引用传递的对象。考虑下面的例子。

It seems like if you a iterating thru a list and pass an object in its actually passed by reference. Consider the following example.

示例

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内部:1 ::

  WithoutRef SetFoo帖子:1 WithoutRef

  SetFoo pre:1 :: WithoutRef SetFoo

  内部:1 :: WithoutRef :: WithRef

  SetFoo帖子:1 WithoutRef :: WithRef

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

SetFoo pre:2 SetFoo内部:2 ::

  WithoutRef SetFoo帖子:2 WithoutRef

  SetFoo pre:2 :: WithoutRef SetFoo

  内部:2 :: WithoutRef :: WithRef

  SetFoo帖子:2 WithoutRef :: WithRef

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

设置间隔pre:0设置间隔内:2 SetIPost:0

SetI Pre: 0 SetI Inside: 2 SetIPost: 0

SetRefI pre:0 SetRefI内部:3

  SetRefI帖子:3

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

我理解的整数例如裁判而不是上述Foo对象迭代的例子。

I understand the ref with the integer example but not the above Foo object iteration example.

谢谢!

推荐答案

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

The reference is passed by value. So the method can still change the contents of the object, it just can't change which object your variable refers to.

请参阅我的文章参数传递,了解更多的信息,连同我对的引用类型和值类型的。

See my article on parameter passing for a lot more information, along with my article about reference types and value types.

这篇关于路过的ref和out的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆