關於C#中三個關鍵字params,Ref,out的詳細介紹
本文主要討論params關鍵字,ref關鍵字,out關鍵字。非常不錯,具有參考借鑒價值,需要的朋友參考下吧
關於這三個關鍵字之前可以研究一下原本的一些操作
using System; using System.Collections.Generic; using System.Text; namespace ParamsRefOut { class Program { static void ChangeValue(int i) { i=5; Console.WriteLine("The ChangeValue method changed the value "+i.ToString()); } static void Main(string[] args) { int i = 10; Console.WriteLine("The value of I is "+i.ToString()); ChangeValue(i); Console.WriteLine("The value of I is " + i.ToString()); Console.ReadLine(); } } }
觀察運行結果發現
值並沒有被改變,也就是說此時的操作的原理可能也是跟以前C語言的函數操作是一樣的
本文主要討論params關鍵字,ref關鍵字,out關鍵字。
1)params關鍵字,官方給出的解釋為用於方法參數長度不定的情況。有時候不能確定一個方法的方法參數到底有多少個,可以使用params關鍵字來解決問題。
using System; using System.Collections.Generic; using System.Text; namespace ParamsRefOut { class number { public static void UseParams(params int [] list) { for(int i=0;i<list.Length;i++) { Console.WriteLine(list[i]); } } static void Main(string[] args) { UseParams(1,2,3); int[] myArray = new int[3] {10,11,12}; UseParams(myArray); Console.ReadLine(); } } }
2)ref關鍵字:使用引用類型參數,在方法中對參數所做的任何更改都會反應在該變數中
using System; using System.Collections.Generic; using System.Text; namespace ParamsRefOut { class number { static void Main() { int val = 0; Method(ref val); Console.WriteLine(val.ToString()); } static void Method(ref int i) { i = 44; } } }
3) out 關鍵字:out 與ref相似但是out 無需進行初始化。
以上是關於C#中三個關鍵字params,Ref,out的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

使用 C# 的 Active Directory 指南。在這裡,我們討論 Active Directory 在 C# 中的介紹和工作原理以及語法和範例。

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。
