C# 透過引用調用

WBOY
發布: 2024-09-03 15:26:32
原創
1013 人瀏覽過

引用參數用於引用變數的記憶體位置。與需要新儲存位置的值參數不同,引用參數表示與作為參數傳遞給方法的原始參數相同的記憶體位置。在C#中,我們使用關鍵字“ref”來聲明引用參數。當我們將引用參數作為參數傳遞給 C# 中的函數時,我們傳遞的是對記憶體位置的引用而不是原始值。我們在 C# 中將此概念稱為「按引用呼叫」。

文法

ref data_typevariable_name
登入後複製

其中 data_type 是變數名稱為變數的資料型態。

C# 引用呼叫的工作原理

  • 每當需要呼叫一個接受參數的函數時,如果我們想要將記憶體位置中變數的參考位址作為參數傳遞給函數,我們在 C# 中使用按引用呼叫。
  • 引用參數是接收記憶體中變數位址的參數。

範例

下面給出了提到的範例:

範例#1

示範按引用呼叫的 C# 程序,其中我們計算數字的平方並在按引用呼叫函數之前和呼叫函數之後顯示值。

代碼:

using System;
//a namespace called program1 is defined
namespace program1
{
//a class called check is defined
class check
{
//a function is defined which takes reference variable as an argument
public void displaypower(ref double value)
{
//the square of the passed value is found using pow method
double power = Math.Pow(value,2);
//The resulting value is added to the value passed as reference
value = value + power;
Console.WriteLine("Value when the control is inside the function "+value);
}
//main method is called
static void Main(string[] args)
{
//a double variable is defined
double value = 5;
//an instance of the check class is defined which consists of the function taking reference parameter as an argument
check check1 = new check();
Console.WriteLine("Value before the function is called "+value);
//a function is called by reference
check1.displaypower(ref value);
Console.WriteLine("The value of the variable remains the same as inside the function because we are calling the function by reference " + value);
}
}
}
登入後複製

輸出:

C# 透過引用調用

說明:

  • 在給定的程式中,我們定義命名空間「program1」。在這個命名空間內,我們有一個名為「check」的類,其中包含一個函數。函數採用引用變數作為參數。
  • 函數以傳遞的值作為參考,計算該值的平方並將其與實際值相加,然後顯示結果值。

範例#2

示範按引用呼叫的 C# 程序,其中我們透過引用呼叫函數並將小寫字母字串作為引用參數傳遞,將給定的小寫字母字串轉換為大寫字母。

代碼:

using System;
//a namespace called program1 is defined
namespace program1
{
//a class called check is defined
class check
{
//a function is defined which takes reference variable as an argument
public void displayupper(ref string value)
{
//ToUpper method is used to convert the string from small letters to capital letters
value = value.ToUpper();
Console.WriteLine("Value when the control is inside the function "+value);
}
//main method is called
static void Main(string[] args)
{
//a double variable is defined
string value = "shobha";
//an instance of the check class is defined which consists of the function taking reference parameter as an argument
check check1 = new check();
Console.WriteLine("Value before the function is called "+value);
//a function is called by reference
check1.displayupper(ref value);
Console.WriteLine("The value of the variable remains the same as inside the function because we are calling the function by reference " + value);
}
}
}
登入後複製

輸出:

C# 透過引用調用

說明:

  • 在上面的程式中,我定義了一個名為「program1」的命名空間。然後,我在這個命名空間中定義一個名為「check」的類別。在「check」類別中,我定義了一個函數,該函數採用引用變數作為參數。
  • 該函數將傳遞的值作為引用,並使用 ToUpper() 方法將給定的小寫字母字串轉換為大寫字母。

推薦文章

這是 C# Call By Reference 的指南。在這裡,我們透過工作和程式設計範例來討論介紹。您也可以查看以下文章以了解更多資訊 –

  1. C# 字典
  2. DataReader C#
  3. IEnumerable C#
  4. C# System.IO

以上是C# 透過引用調用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!