溫馨提示×

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

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

c#函數(shù)中ref/out如何重載

發(fā)布時(shí)間:2020-06-14 20:22:18 來(lái)源:網(wǎng)絡(luò) 閱讀:247 作者:2shoubentian 欄目:編程語(yǔ)言

•                    ref / out 在大部分情況下是標(biāo)識(shí)的一部分!
       你可以重載一個(gè)ref型參數(shù)和一個(gè)普通參數(shù)
       你可以重載一個(gè)out型參數(shù)和一個(gè)普通參數(shù)
       你不可以重載一個(gè)ref型參數(shù)和一個(gè)out型參數(shù)
sealed class Overloading
{
    void Allowed(    int parameter)
    { ... }
    void Allowed(ref int parameter)
    { ... }
   //正確,重載一個(gè)ref型參數(shù)和一個(gè)普通參數(shù)
 
    void AlsoAllowed(    int parameter)
    { ... }
    void AlsoAllowed(out int parameter)
{ ... }
//正確,重載一個(gè)out型參數(shù)和一個(gè)普通參數(shù)

    void NotAllowed(ref int parameter)
    { ... }
    void NotAllowed(out int parameter)
{ ... }
//錯(cuò)誤,不能重載一個(gè)ref型參數(shù)和一個(gè)out型參數(shù)
}
ref和out修飾符可以是一個(gè)函數(shù)的標(biāo)識(shí)。但是你不能同時(shí)重載ref和out型參數(shù)。ref和out修飾符在某種意義上是“安全的“,因?yàn)橹挥衦ef型實(shí)參才能傳遞給ref型函數(shù)參數(shù),只有out型實(shí)參才能傳遞給out型函數(shù)參數(shù)。但是,當(dāng)調(diào)用函數(shù)的時(shí)候,你會(huì)非常容易忘記ref和out修飾符,所以最好不要重載ref和out型參數(shù)。例如:
  sealed class Overloading
  {
   public static void Example(int parameter)
   { ... }
   public static void Example(ref int parameter)
   { ... }
   static void Main()
   {
    int argument = 42;
    Example(argument);//在這兒非常容易忘記ref修飾符
   }
  }
 

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

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

AI