參數修飾符ref,out ,params的區別

來源:互聯網
上載者:User

C#中有三個關鍵字-ref,out ,params,雖然本人不喜歡這三個關鍵字,因為它們疑似破壞物件導向特性。但是既然m$把融入在c#體系中,那麼我們就來認識一下參數修飾符ref,out ,params吧,還有它們的區別。

NO.1 params
一個可以讓方法(函數)的擁有可變參數的關鍵字。

原則:在方法聲明中的 params 關鍵字之後不允許任何其他參數,並且在方法聲明中只允許一個 params 關鍵字。

樣本(拷貝到vs2005中即可用,下面不再說明)
public partial class Form1 : Form
{
    public static void UseParams(params int[] list)
    {
        string temp = "";
        for (int i = 0; i < list.Length; i++)
            temp = temp +" " +list[i].ToString();
        MessageBox.Show(temp);
    }

    public static void UseParams2(params object[] list)
    {
        string temp = "";
        for (int i = 0; i < list.Length; i++)
            temp = temp + " " + list[i].ToString();
        MessageBox.Show(temp);
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        UseParams(1, 2, 3); //看參數是3個
        UseParams(1, 2); //看參數是2個,可變吧
        UseParams2(1, 'a', "test");
        int[] myarray = new int[3]{ 10, 11, 12 };
        UseParams(myarray); //看也可以是容器類,可變吧:)
    }
}

NO.2 out
這是一個引用傳遞L。
原則一:當一個方法(函數)在使用out作為參數時,在方法中(函數)對out參數所做的任何更改都將反映在該變數中。
原則二:當希望方法返回多個值時,聲明 out 方法非常有用。使用 out 參數的方法仍然可以返回一個值。一個方法可以有一個以上的 out 參數。
原則三:若要使用 out 參數,必須將參數作為 out 參數顯式傳遞到方法。out 參數的值不會傳遞到 out 參數。
原則四:不必初始化作為 out 參數傳遞的變數,因為out 參數在進入方法(函數)時後清空自己,使自己變成一個乾淨的參數,也因為這個原因必須在方法返回之前為 out 參數賦值(只有地址沒有值的參數是不能被.net接受的)。
原則五:屬性不是變數,不能作為 out 參數傳遞。
原則六:如果兩個方法的聲明僅在 out 的使用方面不同,則會發生重載。不過,無法定義僅在 ref 和 out 方面不同的重載。例如,以下重載聲明是有效:
class MyClass
{
    public void MyMethod(int i) {i = 10; }
    public void MyMethod(out int i) {i = 10; }
}
而以下重載聲明是無效的:
class MyClass
{
    public void MyMethod(out int i) {i = 10; }
    public void MyMethod(ref int i) {i = 10; }
}
有關傳遞數組的資訊,請參見使用 ref 和 out 傳遞數組。
樣本附後

NO.3 ref
ref僅僅是一個地址!!!
原則一:當一個方法(函數)在使用ref作為參數時,在方法中(函數)對ref參數所做的任何更改都將反映在該變數中。
原則二:調用方法時,在方法中對參數所做的任何更改都將反映在該變數中。
原則三:若要使用 ref 參數,必須將參數作為 ref 參數顯式傳遞到方法。ref 參數的值可以被傳遞到 ref 參數。
原則四:ref參數傳遞的變數必須初始化,因為ref參數在進入方法(函數)時後還是它自己,它這個地址指向的還是原來的值,也因為這個原因ref參數也可以在使用它的方法內部不操作。
原則六:如果兩種方法的聲明僅在它們對 ref 的使用方面不同,則將出現重載。但是,無法定義僅在 ref 和 out 方面不同的重載。例如,以下重載聲明是有效:
class MyClass
{
    public void MyMethod(int i) {i = 10; }
    public void MyMethod(ref int i) {i = 10; }
}
但以下重載聲明是無效的:
class MyClass
{
    public void MyMethod(out int i) {i = 10; }
    public void MyMethod(ref int i) {i = 10; }
}
有關傳遞數組的資訊,請參見使用 ref 和 out 傳遞數組。
樣本

public static string TestOut(out string i)
{
    i = "out b";
    return "return value";
}

public static void TestRef(ref string i)
{
    //改變參數
    i = "ref b";
}

public static void TestNoRef(string refi)
{
    // 不用改變任何東西,這個太明顯了
    refi = "on c";
}

public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    string outi; //不需要初始化
    MessageBox.Show(TestOut(out outi)); //傳回值
    //輸出"return value";
    MessageBox.Show(outi); //調用後的out參數
    //輸出"out b";

    string refi = "a"; // 必須初始化
    TestRef(ref refi); // 調用參數
    MessageBox.Show(refi);
    //輸出"ref b";
    TestNoRef(refi); //不使用ref
    MessageBox.Show(refi);
    //輸出"ref b";

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.