快速了解c# 常量(整数常量,字符常量,定义长量)
- 作者: 达?矢抾哆拉?
- 来源: 51数据库
- 2021-08-27
常量是固定值,程序执行期间不会改变。常量可以是任何基本数据类型,比如整数常量、浮点常量、字符常量或者字符串常量,还有枚举常量。
常量可以被当作常规的变量,只是它们的值在定义后不能被修改。
整数常量
整数常量可以是十进制、八进制或十六进制的常量。前缀指定基数:0x 或 0x 表示十六进制,0 表示八进制,没有前缀则表示十进制。
整数常量也可以有后缀,可以是 u 和 l 的组合,其中,u 和 l 分别表示 unsigned 和 long。后缀可以是大写或者小写,多个后缀以任意顺序进行组合。
这里有一些整数常量的实例:
212 /* 合法 */
215u /* 合法 */
0xfeel /* 合法 */
078 /* 非法:8 不是一个八进制数字 */
032uu /* 非法:不能重复后缀 */
以下是各种类型的整数常量的实例:
85 /* 十进制 */
0213 /* 八进制 */
0x4b /* 十六进制 */
30 /* int */
30u /* 无符号 int */
30l /* long */
30ul /* 无符号 long */
浮点常量
一个浮点常量是由整数部分、小数点、小数部分和指数部分组成。您可以使用小数形式或者指数形式来表示浮点常量。
这里有一些浮点常量的实例:
3.14159 /* 合法 */
314159e-5l /* 合法 */
510e /* 非法:不完全指数 */
210f /* 非法:没有小数或指数 */
.e55 /* 非法:缺少整数或小数 */
使用小数形式表示时,必须包含小数点、指数或同时包含两者。使用指数形式表示时,必须包含整数部分、小数部分或同时包含两者。有符号的指数是用 e 或 e 表示的。
字符常量
字符常量是括在单引号里,例如,'x',且可存储在一个简单的字符类型变量中。一个字符常量可以是一个普通字符(例如 'x')、一个转义序列(例如 '\t')或者一个通用字符(例如 '?')。
在 c# 中有一些特定的字符,当它们的前面带有反斜杠时有特殊的意义,可用于表示换行符(\n)或制表符 tab(\t)。在这里,列出一些转义序列码:
| 转义序列 | 含义 |
| \\ | \ 字符 |
| \' | ' 字符 |
| " | " 字符 |
| \? | ? 字符 |
| \a | alert 或 bell |
| \b | 退格键(backspace) |
| \f | 换页符(form feed) |
| \n | 换行符(newline) |
| \r | 回车 |
| \t | 水平制表符 tab |
| \v | 垂直制表符 tab |
| \ooo | 一到三位的八进制数 |
| \xhh . . . | 一个或多个数字的十六进制数 |
以下是一些转义序列字符的实例:
namespace escapechar
{
class program
{
static void main(string[] args)
{
console.writeline("hello\tworld\n\n");
console.readline();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
hello world
字符串常量
字符串常量是括在双引号 "" 里,或者是括在 @"" 里。字符串常量包含的字符与字符常量相似,可以是:普通字符、转义序列和通用字符
使用字符串常量时,可以把一个很长的行拆成多个行,可以使用空格分隔各个部分。
这里是一些字符串常量的实例。下面所列的各种形式表示相同的字符串。
string a = "hello, world"; // hello, world string b = @"hello, world"; // hello, world string c = "hello \t world"; // hello world string d = @"hello \t world"; // hello \t world string e = "joe said "hello" to me"; // joe said "hello" to me string f = @"joe said ""hello"" to me"; // joe said "hello" to me string g = "\\\\server\\share\\file.txt"; // \\server\share\file.txt string h = @"\\server\share\file.txt"; // \\server\share\file.txt string i = "one\r\ntwo\r\nthree"; string j = @"one two three";
定义常量
常量是使用 const 关键字来定义的 。定义一个常量的语法如下:
const <data_type> <constant_name> = value;
下面的代码演示了如何在程序中定义和使用常量:
using system;
public class consttest
{
class sampleclass
{
public int x;
public int y;
public const int c1 = 5;
public const int c2 = c1 + 5;
public sampleclass(int p1, int p2)
{
x = p1;
y = p2;
}
}
static void main()
{
sampleclass mc = new sampleclass(11, 22);
console.writeline("x = {0}, y = {1}", mc.x, mc.y);
console.writeline("c1 = {0}, c2 = {1}",
sampleclass.c1, sampleclass.c2);
}
}
当上面的代码被编译和执行时,它会产生下列结果:
x = 11, y = 22
c1 = 5, c2 = 10
以上就是快速了解c# 常量的详细内容,更多关于c# 常量的资料请关注其它相关文章!
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
