溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#關(guān)鍵字in、out、ref的作用與區(qū)別是什么

發(fā)布時(shí)間:2022-04-18 10:13:59 來源:億速云 閱讀:341 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“C#關(guān)鍵字in、out、ref的作用與區(qū)別是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“C#關(guān)鍵字in、out、ref的作用與區(qū)別是什么”吧!

簡(jiǎn)介:

In:過程不會(huì)改寫In的內(nèi)容 ,默認(rèn)的傳遞方式,即向函數(shù)內(nèi)部傳送值。
Out和out:傳入的值不會(huì)被過程所讀取,Out在傳入的時(shí)候,參數(shù)的數(shù)值會(huì)清空,但過程可以寫 。只出不進(jìn)
ref:可以把參數(shù)的數(shù)值傳遞進(jìn)函數(shù) ,過程會(huì)讀,會(huì)寫 。有進(jìn)有出。

一、In

In 關(guān)鍵字使參數(shù)按值傳遞。即向函數(shù)內(nèi)部傳送值。

例如:

using System;
class gump
{
    public double square(double x)
    {
        x=x*x;
        return x;
    }
}

class TestApp
{
    public static void Main()
    {
        gump doit=new gump();

        double a=3;
        double b=0;

        Console.WriteLine(\"Before square->a={0},b={1}\",a,b);//a=3,b=0;

            b=doit.square( a);
        Console.WriteLine(\"After square->a={0},b={1}\",a,b);//a=3,b=9;
    }
}

二、ref

ref 關(guān)鍵字使參數(shù)按引用傳遞。其效果是,當(dāng)控制權(quán)傳遞回調(diào)用方法時(shí),在方法中對(duì)參數(shù)的任何更改都將反映在該變量中。若要使用 ref 參數(shù),則方法定義和調(diào)用方法都必須顯式使用 ref 關(guān)鍵字。

例如:

using System;
class gump
{
    public double square(double x)
    {
        x=x*x;
        return x;
    }
}

class TestApp
{
    public static void Main()
    {
        gump doit=new gump();

        double a=3;
        double b=0;

        Console.WriteLine(\"Before square->a={0},b={1}\",a,b);//a=3,b=0;

            b=doit.square( ref a);
        Console.WriteLine(\"After square->a={0},b={1}\",a,b);//a=9,b=9;
    }
}

傳遞到 ref 參數(shù)的參數(shù)必須最先初始化。這與 out 不同,后者的參數(shù)在傳遞之前不需要顯式初始化。

三、out

out 關(guān)鍵字會(huì)導(dǎo)致參數(shù)通過引用來傳遞。這與 ref 關(guān)鍵字類似,不同之處在于 ref 要求變量必須在傳遞之前進(jìn)行初始化。若要使用 out 參數(shù),方法定義和調(diào)用方法都必須顯式使用out 關(guān)鍵字。

using System;
class gump
{
    public void math_routines(double x,out double half,out double squared,out double cubed)
//可以是:public void math_routines(ref double x,out double half,out double squared,out double cubed)
//但是,不可以這樣:public void math_routines(out double x,out double half,out double squared,
//out double cubed),對(duì)本例來說,因?yàn)檩敵龅闹狄縳賦值,所以x不能再為輸出值
    {
        half=x/2;
        squared=x*x;
        cubed=x*x*x;
    }
}

class TestApp
{
    public static void Main()
    {
        gump doit=new gump();

        double x1=600;
        double half1=0;
        double squared1=0;
        double cubed1=0;
        [Page]
        /*
        double x1=600;
        double half1;
        double squared1;
        double cubed1;
        */

        Console.WriteLine(\"Before method->x1={0}\",x1);
        Console.WriteLine(\"half1={0}\",half1);          Console.WriteLine(\"squared1={0}\",squared1);
        Console.WriteLine(\"cubed1={0}\",cubed1);



        doit.math_rountines(x1,out half1,out squared1,out cubed1);
  //此時(shí)的Out修飾的參數(shù)值均已經(jīng)發(fā)生改變。
            Console.WriteLine(\"After method->x1={0}\",x1);
            Console.WriteLine(\"half1={0}\",half1);
            Console.WriteLine(\"squared1={0}\",squared1);
            Console.WriteLine(\"cubed1={0}\",cubed1);
           
    }
}

盡管作為 out 參數(shù)傳遞的變量不必在傳遞之前進(jìn)行初始化,但需要調(diào)用方法以便在方法返回之前賦值。

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
 
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

四、區(qū)別:

1.ref和out的區(qū)別在C# 中,既可以通過值也可以通過引用傳遞參數(shù)。通過引用傳遞參數(shù)允許函數(shù)成員更改參數(shù)的值,并保持該更改。若要通過引用傳遞參數(shù), 可使用ref或out關(guān)鍵字。ref和out這兩個(gè)關(guān)鍵字都能夠提供相似的功效,其作用也很像C中的指針變量。

2.使用ref型參數(shù)時(shí),傳入的參數(shù)必須先被初始化。對(duì)out而言,必須在方法中對(duì)其完成初始化。

3.使用ref和out時(shí),在方法的參數(shù)和執(zhí)行方法時(shí),都要加Ref或Out關(guān)鍵字。以滿足匹配。

4.out適合用在需要retrun多個(gè)返回值的地方,而ref則用在需要被調(diào)用的方法修改調(diào)用者的引用的時(shí)候。

5.方法參數(shù)上的 out 方法參數(shù)關(guān)鍵字使方法引用傳遞到方法的同一個(gè)變量。當(dāng)控制傳遞回調(diào)用方法時(shí),在方法中對(duì)參數(shù)所做的任何更改都將反映在該變量中。

感謝各位的閱讀,以上就是“C#關(guān)鍵字in、out、ref的作用與區(qū)別是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)C#關(guān)鍵字in、out、ref的作用與區(qū)別是什么這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI