首页  >  文章  >  后端开发  >  C# 只读

C# 只读

王林
王林原创
2024-09-03 15:31:31171浏览

可以在我们的应用程序中使用 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();
}
}
}

输出:

C# 只读

在上面的程序中,定义了一个名为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();
}
}
}

输出:

C# 只读

在上面的程序中,定义了一个名为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();
}
}
}

输出:

C# 只读

在上面的程序中,定义了一个名为program的命名空间。然后定义一个名为 check 的类,其中定义只读字段来存储字符串和整数值。然后定义一个名为 example 的类,在该类中调用 main 方法。然后调用 main 方法,在该方法中定义类 check 的实例,该实例读取只读字段中存储的值并作为输出打印在屏幕上。输出如上面的快照所示。

结论

在本教程中,我们通过定义了解 C# 中只读关键字的概念、只读语法以及通过编程示例及其输出了解 C# 中只读关键字的工作原理。

以上是C# 只读的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Partial in C#下一篇:C# Action Delegate