更新所以完全拉了一個工具時刻。我的意思是通過參考與出/參考。凡是說 '裁判' 我真的被引用意味着在C#Out參數問題:Out如何處理值類型?
的someMethod(對象someObject)
對戰
的someMethod(出someObject)
對不起。只是不想更改代碼,所以答案已經有意義。
據我所知,不像裁判那裏「複製」指針並創建堆棧上的新空間,使用指針,但不會改變指針:
SomeMethod()
{
SomeThing outer = new SomeThing();
RefMethod(ref outer)
}
RefMethod(ref inner) //new space on stack created and uses same pointer as outer
{
inner.Hi = "There"; //updated the object being pointed to by outer
inner = new SomeThing();//Given a new pointer, no longer shares pointer with outer
//New object on the heap
}
出副本指針,可以操縱它指向的:
SomeMethod()
{
SomeThing outer = new SomeThing();
RefMethod(out outer)
}
RefMethod(out inner) //same pointer shared
{
inner = new SomeThing();//pointer now points to new place on heap
//outer now points to new object
//Old object is orphaned if nothing else points to it
}
這很好,很正常,使用對象,但對於值類型看到,因爲他們沒有什麼指向僅爲堆棧?
我認爲你的意思是一個out參數*有*必須在方法內的正常返回之前明確賦值;一個用作out參數的變量不必在調用之前明確賦值,儘管它可以是。 (這將在之後。) – 2009-01-12 19:53:35
要添加到Jon所說的,ref參數必須被初始化,並且out參數不必是(但可以)。 – 2009-01-12 19:56:49