創作內容

11 GP

C# Call By Reference

作者:貓貓風 ฅ●ω●ฅ│2017-12-30 22:15:59│巴幣:22│人氣:841
.













在 C# 中指標包裝起來,不像C或C++可以直接使用,在C# MSDN的說明中指出

雖然有支援指標功能,但不建議使用,如果發生問題自行負責

指標本來就是不安全的用法,如果隨意使用確實很容易出問題

有上述可見,C#中正常是不會出現指標的用法

但有很多資料結構已經把指標封裝起來,意思就是底層已經被實作

程式撰寫者只需調用該物件即可自行編成邏輯使用

其中  Call By Reference 如同字面上的意思,用位址傳值

當原始位址的值改變,參數的值也會跟著改變

以下範例舉例使用C# 的 out 跟 ref 來演示 Call By Reference


Class Main


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace out_ref_test  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         int[] array = { 1, 2, 3, 4, 5 };  
  15.         int num1 = 999, num2 = 999;  
  16.         float fnum1 = 0, fnum2 = 0;  
  17.         ModifyArray modArr = new ModifyArray();  
  18.   
  19.         public Form1()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.   
  24.         private void Form1_Load(object sender, EventArgs e)  
  25.         {  
  26.             modArr.chage_array(array);  
  27.             modArr.chage_int(ref num1, ref num2);  
  28.             modArr.chage_float(out fnum1, out fnum2);  
  29.   
  30.             for(int i = 0; i < array.Length ; i++)  
  31.             {  
  32.                 if (i == array.Length - 1)  
  33.                 {  
  34.                     textBox1.Text += array[i];  
  35.                 }  
  36.                 else  
  37.                 {  
  38.                     textBox1.Text += array[i] + ", ";  
  39.                 }  
  40.             }  
  41.   
  42.             textBox2.Text += num1 + ", " + num2;  
  43.   
  44.             textBox3.Text += fnum1 + ", " + fnum2;  
  45.         }  
  46.     }  
  47. }  

Class Modify


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace out_ref_test  
  7. {  
  8.     public class Modify  
  9.     {  
  10.         public void chage_array(int[] arr)  
  11.         {  
  12.             arr[2] = 222;  
  13.             arr[3] = 999;  
  14.         }  
  15.   
  16.         public void chage_int(ref int a, ref int b)  
  17.         {  
  18.             a = 100; //如果不初始化則 a++ 不會被執行  
  19.             b = 200;  
  20.             a ++;  
  21.             b ++;  
  22.             /*a = 100;
  23.             b = 200;*/  
  24.         }  
  25.   
  26.         public void chage_float(out float a, out float b)  
  27.         {  
  28.             a = 100.11f;  
  29.             b = 200.22f;  
  30.         }  
  31.     }  
  32. }  





上述範例主要使用三種方式呈現  Call By Reference

第一個是使用陣列,從結果可以看出陣列本身就已經是 Call By Reference

將陣列傳入 Class後改變數值不需回傳,在主程式執行的結果仍會改變


第二和三為使用 ref和 out 來實現在副程式中回傳多個值的用法

ref 和 out 不同的地方為 ref使用前需先初始化,否則會無法正常運作







引用網址:https://home.gamer.com.tw/TrackBack.php?sn=3837690
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:C#

留言共 3 篇留言

小魚
ref 跟 out 是真的蠻實用的,
我發現還蠻常用到的,
感謝貓貓風的分享 :)

12-30 22:47

貓貓風 ฅ●ω●ฅ
[e19]12-30 23:00
小魚
補充一下,
ref 跟 out還有另外一個差別,
如果部分資料都事先處理好,但有些資料要帶進去函式處理,要用ref,
out是要在函式中新建一個物件,處理完之後回傳該物件。

12-30 23:05

貓貓風 ฅ●ω●ฅ
感謝補充12-30 23:05
珀伽索斯(Ama)
哇!這看了有點頭痛XD
不過還是感謝貓貓風分享[e34]

01-07 22:33

我要留言提醒:您尚未登入,請先登入再留言

11喜歡★s1234567 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:人骨成劍 - 奪夫之仇 ... 後一篇:C 二元樹走訪...

追蹤私訊切換新版閱覽

作品資料夾

sakima55詐騙季來囉?
晚上公司老闆說帳號被限額5萬,這麼晚了沒打電話傳訊息感覺詐騙,在知道對方限額是10萬下直接無視。看更多我要大聲說昨天22:42


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】