通过ref和out传递

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

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

class Program { static void Main(string[] args) { List list = new List(); 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 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对象迭代示例。

谢谢!

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

有关更多信息,请参阅我关于参数传递的文章 ,以及有关引用类型和值类型的文章 。