2023-06-28
自我提升
0

常量
常量是固定值,程序执行期间不会改变,它们的值在定义后不能被修改
合法常量

c#
0213 /* 八进制 */ 0x4b /* 十六进制 */ 30 /* int */ 30u /* 无符号 int */ 30l /* long */ 30ul /* 无符号 long */ 3.14159 /* 合法 */ 314159E-5L /* 合法 */ string a = "hello, world"; // hello, world string b = @"hello, world"; // hello, world string c = "hello \t world"; // hello world

字符串常量是括在双引号 "" 里,或者是括在 @"" 里。

非法常量

c#
510E /* 非法:不完全指数 */ 210f /* 非法:没有小数或指数 */ .e55 /* 非法:缺少整数或小数 */ 078 /* 非法:8 不是一个八进制数字 */ 032UU /* 非法:不能重复后缀 */

怎么把⼀个变量变成常量

c#
const <data_type> <constant_name> = value;

为什么需要定义⼀个常量
⽅便使⽤

c#
static void Main(string[] args) { Console.WriteLine("Hello World!"); //变量 常量 int i = 12; i = 20; const int j = 100; const double PI = 3.141592;// double temp = PI * 90; Console.WriteLine(PI); }