欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年04月18日 08:54:51   作者:農(nóng)碼一生  
這篇文章介紹了C#關(guān)鍵字in、out、ref的作用與區(qū)別,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

簡介:

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

一、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ù)按引用傳遞。其效果是,當控制權(quán)傳遞回調(diào)用方法時,在方法中對參數(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)鍵字會導致參數(shù)通過引用來傳遞。這與 ref 關(guān)鍵字類似,不同之處在于 ref 要求變量必須在傳遞之前進行初始化。若要使用 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),對本例來說,因為輸出的值要靠x賦值,所以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);
  //此時的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ù)傳遞的變量不必在傳遞之前進行初始化,但需要調(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這兩個關(guān)鍵字都能夠提供相似的功效,其作用也很像C中的指針變量。

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

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

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

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

以上所述是小編給大家介紹的C#關(guān)鍵字in、out、ref的作用與區(qū)別,希望對大家有所幫助。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論