-
Qualifying a method's formal parameter as const
将一个方法的形参设置为常量
-
You declare a class constant with the const keyword.
用const关键字声明类常量。
-
Embedding const ants in code is obviously foolish.
在代码中嵌入常量显然是愚蠢的。
-
If you don't need to change it, use const reference.
如果你不需要改变它,使用常量引用。
-
Notice that the API_KEY variable is declared as a const.
请注意,API_KEY变量被声明为const。
1. const是一个C语言的关键字,它限定一个变量不允许被改变。使用const在一定程度上可以提高程序的安全性和可靠性。另外,在观看别人代码的时候,清晰理解const所起的作用,对理解对方的程序也有一些帮助。另外CONST在其他编程语言中也有出现,如C++、PHP5、C#.net、HC08C。
2. const 关键字用于修改字段或局部变量的声明。它指定字段或局部变量的值是常数,不能被修改。例如:\n const int x = 0;\n public const double gravitationalConstant = 6.673e-11;\n private const string productName = \Visual C#\;\n 备注\n 常数声明的类型指定声明引入的成员类型。常数表达式必须产生具有目标类型或者可隐式转换为目标类型的类型的值。\n 常数表达式是在编译时可被完全计算的表达式。因此,对于引用类型的常数,可能的值只能是 string 和 null。\n 常数声明可以声明多个常数,例如:\n public const double x = 1.0, y = 2.0, z = 3.0;\n 不允许在常数声明中使用 static 修饰符。\n 常数可以参与常数表达式,如下所示:\n public const int c1 = 5;\n public const int c2 = c1 + 100;\n 注意\n readonly 关键字与 const 关键字不同。const 字段只能在该字段的声明中初始化。readonly 字段可以在声明或构造函数中初始化。因此,根据所使用的构造函数,readonly 字段可能具有不同的值。另外,const 字段是编译时常数,而 readonly 字段可用于运行时常数,如下面的代码行所示:public static readonly uint l1 = (uint)DateTime.Now.Ticks;