目录
1. 简单类型分类
- 数值类型
- 有符号整数类型
- 无符号整数类型
- 浮点数类型
- 布尔类型
- 字符类型
- 字符串类型
2. 简单类型大小
布尔值类型:bool(1 byte)
字符类型:char(2 byte)
浮点数类型:double(8 byte),float(4 byte),decimal(16 byte)
无符号整数类型:byte(1 byte),ushort(2 byte),uint(4 byte),ulong(8 byte),nuint(本机大小)
有符号整数类型:sbyte(1 byte),short(2 byte),int(4 byte),long(8 byte),nint(本机大小)
1 byte = 8 bit
sizeof()函数:产生以字节byte为单位的存储对象或类型的存储尺寸。
3. 整型数值类型
每个整型类型的默认值都为 0
。
4. 浮点数值类型
decimal相对于float和double,表示更高精度的浮点数,但是表示的数据范围比二者都小。
5. 其他简单类型
6. .NET类型
每个简单C#类型关键字都是相应 .NET 类型的别名。 关键字和 .NET 类型名称是可互换的。
using System;
class Program
{
static void Main()
{
string a = "hhh";
System.String b = "hhh";
if(a.GetType() == b.GetType())
{
Console.WriteLine("hhh");
}
}
}
//expected output: hhh
7. 后缀
有些数据类型必须要有后缀。
8. 转义字符
在字符串前加一个@,表示字符串内包括的有转义符的,都按不进行转义来运算,"除外。
using System;
class Program
{
static void Main()
{
string a = @"hhh\n";
Console.WriteLine($"{a}");
}
}
//expected output: hhh\n
文件名尤其需要这种用法。
9. Unicode
Unicode值列是字符在Unicode字符集中的16进制值。使用Unicode值可以替换转义字符中的字符,使用方法为:
\ + u + 4位16进制值
例如:
- \n 与 \u000A 等价
- \r 与 \u000D 等价
- \t 与 \u0009 等价