影片內容說明兩者最大差異在於 ref 參數必須先初始化,而 out 參數不需要
MVA 範例:說明 ref 和 out 都是 ByRef
static void Main(string[] args)
{
string outString = "This is the original outString";
Console.WriteLine(outString);
outMethod(out outString);
Console.WriteLine(outString);
string refString = "This is the original ref string";
Console.WriteLine(refString);
refMethod(ref refString);
Console.WriteLine(refString);
}
static void outMethod(out string outString)
{
outString = "This is the new outString value";
}
static void refMethod(ref string refString)
{
refString = "This is the new refString value";
}
![[C#] ref 和 out 差異-2](https://farm4.staticflickr.com/3923/14591743264_953b048fc1_z.jpg)
![[C#] ref 和 out 差異](https://farm4.staticflickr.com/3913/14382420340_d339be2326_o.jpg)
沒有留言:
張貼留言