星期三, 7月 09, 2014

[C#] ref 和 out 差異

MVA Twenty C# Questions Explained - [​10 What is the difference between ref and out keywords?]

影片內容說明兩者最大差異在於 ref 參數必須先初始化,而 out 參數不需要

[C#] ref 和 out 差異-2

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 差異

沒有留言:

張貼留言