定义常量
常量必须使用 const 关键字定义,并具有以下语法形式:
const data_type constant_name = value;
其中data_type是数据常量的类型,constant_name是常量的名称(类似于变量的名称),value是常量的值。
整数常量
整数常量可以用八进制、十进制或十六进制指定,并且前缀可用于指定特定的基数。例如, 0x或0X表示十六进制, 0表示八进制,没有前缀表示十进制。
除了前缀之外,整数常量还可以包含由 U 和 L 组合而成的后缀。 U 和 L 分别代表无符号和长整型。后缀可以是大写或小写,并且可以按任何顺序组合,但不能重复。
以下是整数常量的一些示例。
85 // Legitimate: decimal constants
0213 // Legitimate: octal constants
0x4b // Legitimate: hexadecimal constants
30 // Legitimate: int type constants
30u // Legitimate: unsigned int type constants
30l // Legitimate: long type constants
30ul // Legitimate: unsigned long type constants
068 // Illegal: 8 is not an octal digit
032UU // Illegal: suffix cannot be repeated
浮点常数
浮点常数由整数部分、小数点、小数部分和指数部分组成。浮点常量可以用十进制或指数格式表示。
以下是浮点常量的一些示例。
3.14159 // Legal
314159E-5L // Legal
510E // Illegal: incomplete exponent
210f // Illegal: no decimal or exponent
.e55 // Illegal: missing integer or decimal
当以十进制格式表示浮点常量时,它必须包含小数点、指数或两者。当您使用指数格式表示浮点常量时,必须包含整数部分、小数部分或两者。有符号指数由 e 或 E 表示。
字符常数
字符常量必须用单引号括起来,类似于定义字符串类型的变量(例如'x' 。字符常量可以是常规字符(例如'x' )、转义序列(例如'\t' )或 Unicode 字符(例如'\u02C0' )。
在 C# 中,某些字符前面有反斜杠并且具有特殊含义。例如, \n代表换行符, \t代表制表符。下表列出了一些常用的转义字符。
| 转义序列 | 意义 |
|---|---|
| \\ | \ 特点 |
| \’ | ‘ 特点 |
| \” | “ 特点 |
| \? | ? 特点 |
| \a | 警报或铃声 |
| \b | 退格键(退格键) |
| \f | 换页 |
| \n | 换行(换行) |
| \r | 回车 |
| \t | 水平制表符 |
| \v | 垂直制表符 |
| \ooo | 1 到 3 个八进制字符 |
| \xhh . . . | 一个或多个十六进制数字 |
以下示例显示了转义字符的使用。
using System;
namespace it-kiso.com
{
class Demo
{
static void Main(string[] args){
Console.WriteLine("IT基礎\thttps://it-kiso.com\n\n");
Console.ReadLine();
}
}
}
操作的结果将是:
IT基礎 https://it-kiso.com

字符串常量必须用双引号" "或@" "括起来。字符常量与字符常量一样,可以是普通字符、转义序列或 Unicode 字符。
下面列出了一些字符串常量。 (下面的各种格式字符串常量都代表同一个字符串)
“IT基礎”;
@”IT基礎”;




![2021 年如何设置 Raspberry Pi Web 服务器 [指南]](https://i0.wp.com/pcmanabu.com/wp-content/uploads/2019/10/web-server-02-309x198.png?w=1200&resize=1200,0&ssl=1)

