C# 只讀
可以在我們的應用程式中使用 C# 上的關鍵字 read-only 來定義唯讀字段,並且可以在聲明期間或建構函數中完成只讀字段值的初始化。使用關鍵字read-only 定義的唯讀欄位的評估是在執行時間完成的,並且該唯讀關鍵字可以與字串、數字、空引用或布林值一起使用,並且每當欄位定義為唯讀時只是,如果定義該欄位的建構函數執行結束,則該欄位的值不能更改,並且不建議對值可以隨時更改的欄位使用唯讀關鍵字。在本主題中,我們將學習 C# readonly。
文法:
readonly data_type field_name = "value";
其中 data_type 是唯讀欄位的資料類型,
field_name 是欄位的名稱。
C# 中唯讀的工作方式
以下是工作原理:
- 每當需要定義唯讀欄位時。我們在 C# 中使用唯讀關鍵字。
- 只讀欄位值的初始化可以在宣告期間或建構函式中完成。
- 使用關鍵字 read-only 定義的唯讀欄位的評估是在執行時完成的。
- 唯讀關鍵字可以與字串、數字、空引用或布林值一起使用。
- 每當欄位定義為唯讀時,如果定義該欄位的建構函式執行結束,則該欄位的值無法變更。
- 對於值可能隨時變更的字段,不建議使用唯讀關鍵字。
C# 只讀範例
在這裡我們討論下面提到的以下範例」
範例#1
C#程式示範只讀字段,讀取只讀字段中儲存的值。
代碼:
using System.IO; using System; //a namespace called program is defined namespace program { //a class called check is defined within which the read only field is defined to store the string class check { public readonly string stringname = "Welcome to C Sharp"; } //a class called example is defined within which the main method is called class example { //main method is called within which the instance of the class check is defined to which reads the value stored in the read only field and prints as output on the screen static void Main(string[] args) { check checkvar = new check(); Console.WriteLine(checkvar.stringname); Console.ReadLine(); } } }
輸出:
在上面的程式中,定義了一個名為program的命名空間。然後定義一個名為 check 的類,其中定義了唯讀欄位來儲存字串。然後定義一個名為 example 的類,在其中呼叫 main 方法。然後呼叫 main 方法,在該方法中定義類別 check 的實例,該實例讀取儲存在唯讀欄位中的值並作為輸出列印在螢幕上。輸出如上面的快照所示。
範例#2
C#程式示範只讀字段,讀取只讀字段中儲存的值。
代碼:
using System.IO; using System; //a namespace called program is defined namespace program { //a class called check is defined within which the read only field is defined to store the double value class check { public readonly double num = 10.50; } //a class called example is defined within which the main method is called class example { //main method is called within which the instance of the class check is defined to which reads the value stored in the read only field and prints as output on the screen static void Main(string[] args) { check checkvar = new check(); Console.WriteLine("The value of the variable is: {0}",checkvar.num); Console.ReadLine(); } } }
輸出:
在上面的程式中,定義了一個名為program的命名空間。然後定義一個名為 check 的類,其中定義唯讀欄位來儲存 double 值。然後定義一個名為 example 的類,在其中呼叫 main 方法。然後呼叫 main 方法,在該方法中定義了類別 check 的實例,該實例讀取唯讀欄位中儲存的值並作為輸出列印在螢幕上。輸出如上面的快照所示。
範例#3
C# 程式示範讀取唯一欄位以讀取唯讀欄位中儲存的值。
代碼:
using System.IO; using System; //a namespace called program is defined namespace program { //a class called check is defined within which the read only field is defined to store the double value class check { public readonly string authorname = "Shobha Shivakumar"; public readonly string bookname = "Meaning of life"; public readonly int publishingyear = 2020; } //a class called example is defined within which the main method is called class example { //main method is called within which the instance of the class check is defined to which reads the value stored in the read only field and prints as output on the screen static void Main(string[] args) { check checkvar = new check(); Console.WriteLine("The name of the author is: {0}",checkvar.authorname); Console.WriteLine("The name of the book is: {0}",checkvar.bookname); Console.WriteLine("The publishing year of the book is: {0}",checkvar.publishingyear); Console.ReadLine(); } } }
輸出:
在上面的程式中,定義了一個名為program的命名空間。然後定義一個名為 check 的類,其中定義唯讀欄位來儲存字串和整數值。然後定義一個名為 example 的類,在該類別中呼叫 main 方法。然後呼叫 main 方法,在該方法中定義類別 check 的實例,該實例讀取只讀欄位中儲存的值並作為輸出列印在螢幕上。輸出如上面的快照所示。
結論
在本教程中,我們透過定義來了解 C# 中只讀關鍵字的概念、只讀語法以及透過程式設計範例及其輸出來了解 C# 中只讀關鍵字的工作原理。
以上是C# 只讀的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

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

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

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

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

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

Dreamweaver CS6
視覺化網頁開發工具

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

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

C#和C 的歷史與演變各有特色,未來前景也不同。 1.C 由BjarneStroustrup在1983年發明,旨在將面向對象編程引入C語言,其演變歷程包括多次標準化,如C 11引入auto關鍵字和lambda表達式,C 20引入概念和協程,未來將專注於性能和系統級編程。 2.C#由微軟在2000年發布,結合C 和Java的優點,其演變注重簡潔性和生產力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注於開發者的生產力和雲計算。

C# 多線程編程是一種讓程序同時執行多項任務的技術,它可以通過提升性能、提高響應能力和實現並行處理來提高程序效率。雖然 Thread 類提供了直接創建線程的方法,但 Task 和 async/await 等高級工具可以提供更安全的異步操作和更簡潔的代碼結構。多線程編程中常見的難題包括死鎖、競態條件和資源洩漏,需要仔細設計線程模型和使用適當的同步機制來避免這些問題。

如何利用.NET構建應用?使用.NET構建應用可以通過以下步驟實現:1)了解.NET基礎知識,包括C#語言和跨平台開發支持;2)學習核心概念,如.NET生態系統的組件和工作原理;3)掌握基本和高級用法,從簡單控制台應用到復雜的WebAPI和數據庫操作;4)熟悉常見錯誤與調試技巧,如配置和數據庫連接問題;5)應用性能優化與最佳實踐,如異步編程和緩存。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

多線程的好處在於能提升性能和資源利用率,尤其適用於處理大量數據或執行耗時操作。它允許同時執行多個任務,提高效率。然而,線程過多會導致性能下降,因此需要根據 CPU 核心數和任務特性謹慎選擇線程數。另外,多線程編程涉及死鎖和競態條件等挑戰,需要使用同步機制解決,需要具備紮實的並發編程知識,權衡利弊並謹慎使用。

.NETFramework是一個軟件框架,C#是一種編程語言。 1..NETFramework提供庫和服務,支持桌面、Web和移動應用開發。 2.C#設計用於.NETFramework,支持現代編程功能。 3..NETFramework通過CLR管理代碼執行,C#代碼編譯成IL後由CLR運行。 4.使用.NETFramework可快速開發應用,C#提供如LINQ的高級功能。 5.常見錯誤包括類型轉換和異步編程死鎖,調試需用VisualStudio工具。

如何將C#.NET應用部署到Azure或AWS?答案是使用AzureAppService和AWSElasticBeanstalk。 1.在Azure上,使用AzureAppService和AzurePipelines自動化部署。 2.在AWS上,使用AmazonElasticBeanstalk和AWSLambda實現部署和無服務器計算。
