// 在 C# 中定义常量可以使用 const 或 readonly 关键字。const 用于编译时常量,而 readonly 用于运行时常量。
// 使用 const 定义编译时常量
public class ConstantsExample
{
// 定义一个编译时常量 PI
public const double PI = 3.14159;
// 定义一个编译时常量 MaxValue
public const int MaxValue = 100;
}
// 使用 readonly 定义运行时常量
public class ReadOnlyExample
{
// 定义一个运行时常量
public readonly DateTime StartTime;
// 构造函数中初始化 readonly 变量
public ReadOnlyExample()
{
StartTime = DateTime.Now;
}
}
int
, double
, string
等)或枚举类型。上一篇:c# using()
下一篇:c# linq select
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站