可以在我们的应用程序中使用 C# 上的关键字 read-only 来定义只读字段,并且可以在声明期间或构造函数中完成只读字段值的初始化。使用关键字 read-only 定义的只读字段的评估是在运行时完成的,并且该只读关键字可以与字符串、数字、空引用或布尔值一起使用,并且每当字段定义为只读时只是,如果定义该字段的构造函数执行结束,则该字段的值不能更改,并且不建议对值可以随时更改的字段使用只读关键字。在本主题中,我们将学习 C# readonly。
语法:
readonly data_type field_name = "value";
其中 data_type 是只读字段的数据类型,
field_name 是字段的名称。
以下是工作原理:
在这里我们讨论下面提到的以下示例”
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 的实例,该实例读取存储在只读字段中的值并作为输出打印在屏幕上。输出如上面的快照所示。
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 的实例,该实例读取只读字段中存储的值并作为输出打印在屏幕上。输出如上面的快照所示。
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中文网其他相关文章!