变量是保存数据的存储容器。
1、数据类型
变量若按范围分类,可以分为:局部变量,全局变量
按成员分类:成员变量,非成员变量
按内存分配分类: 值类型变量,引用类型变量
值类型
存储在栈内存区的数据称为值类型, 继承(派生)于 System.ValueType 类,而 ValueType 继承(派生)于 System.Object, 正所谓 一切皆为object(对象)
类型 | 描述 | 范围 | 默认值 | 全称 |
bool | 布尔值 | true或者false | false | System.Boolean |
byte | 8位无符号整数 | 0到255 | 0 | System.Byte |
char | 16位Unicode字符 | U+0000到U+ffff | '\0' | System.Char |
decimal | 128位精确的十进制,28位-29位有效位数 | (-7.9x1028到7.9x1028)/100到28 | 0.0M | System.Decimal |
double | 64位双精度浮点型 | (+/-)5.0x10-324到(+/-)1.7x10308 | 0.0D | System.Double |
float | 32位有符号整数类型 | -3.4x1038到+3.4x1038 | 0.0F | System.Single |
int | 32位有符号整数类型 | -2,147,483,648到2,147,483,647 | 0 | System.Int32 |
long | 64位有符号整数类型 |
-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 | 0L | System.Int64 |
sbyte | 8位有符号整数类型 | -128到127 | 0 | System.SByte |
short | 16位有符号整数类型 | -32768到32767 | 0 | System.Int16 |
uint | 32位无符号整数类型 | 0到4,294,967,295 | 0 | System.UInt32 |
ulong | 64位无符号整数类型 | 0到18,446,744,073,709,551,615 | 0 | System.UInt64 |
ushort | 16位无符号整数类型 | 0到65535 | 0 | System.UInt16 |
enum | 枚举数列表的常量组成的特殊数据类型 | System.Enum | ||
struct | 结构体,封装小型相关变量组,不常用 |
不常用的数据类型: sbyte,uint,ulong,ushort,struct
引用类型
继承(派生)于 System.Object 类
存储在堆内存区的数据称为引用类型。
声明引用类型的关键字:class(类), interface(接口), delegate(委托)
C# 9.0 推出了 record( 记录) 类型.
内置引用类型:dynamic, object, string