日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

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

作者:農(nóng)碼一生 ? 更新時(shí)間: 2022-06-17 編程語(yǔ)言

簡(jiǎn)介:

In:過(guò)程不會(huì)改寫In的內(nèi)容 ,默認(rèn)的傳遞方式,即向函數(shù)內(nèi)部傳送值。
Out和out:傳入的值不會(huì)被過(guò)程所讀取,Out在傳入的時(shí)候,參數(shù)的數(shù)值會(huì)清空,但過(guò)程可以寫 。只出不進(jìn)
ref:可以把參數(shù)的數(shù)值傳遞進(jìn)函數(shù) ,過(guò)程會(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ù)通過(guò)引用來(lái)傳遞。這與?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ì)本例來(lái)說(shuō),因?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# 中,既可以通過(guò)值也可以通過(guò)引用傳遞參數(shù)。通過(guò)引用傳遞參數(shù)允許函數(shù)成員更改參數(shù)的值,并保持該更改。若要通過(guò)引用傳遞參數(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ù)所做的任何更改都將反映在該變量中。

原文鏈接:https://www.cnblogs.com/wml-it/p/14776395.html

欄目分類
最近更新