聊一聊C#基本类型
C#基本类型
闲来无事,重新温习了下C#基本类型。以下讲的基本类型主要是包括基本的值类型类型和string。struct和class不包含其中。
C#基本类型------值类型:
bool,byte,sbyte,char,short,ushort,int,uint,long,ulong,float,double,decimal
特殊值类型:
时间类型,枚举类型实为系统定义的结构类型。
C#基本类型-------引用类型:
string,object,各类可空类型,Array
值类型的信息
bool -> System.Boolean :布尔型,其值为 true 或者 false,占用4个字节;
byte -> System.Byte :字节型,占 1 字节,表示 8 位正整数,范围 0 ~ 255;
sbyte -> System.SByte :带符号字节型,占 1 字节,表示 8 位整数,范围 -128 ~ 127;
char -> System.Char :字符型,占有两个字节,表示 1 个 Unicode 字符;
short -> System.Int16 :短整型,占 2 字节,表示 16 位整数,范围 -32,768 ~ 32,767;
ushort -> System.UInt16 :无符号短整型,占 2 字节,表示 16 位正整数,范围 0 ~ 65,535;
uint -> System.UInt32 :无符号整型,占 4 字节,表示 32 位正整数,范围 0 ~ 4,294,967,295;
int -> System.Int32 :整型,占 4 字节,表示 32 位整数,范围 -2,147,483,648 到 2,147,483,647;
ulong -> System.UInt64 :无符号长整型,占 8 字节,表示 64 位正整数,范围 0 ~ 大约 10 的 20 次方;
long -> System.Int64 :长整型,占 8 字节,表示 64 位整数,范围大约 -(10 的 19) 次方 到 10 的 19 次方;
float -> System.Single :单精度浮点型,占 4 个字节,范围-3.4 × 10的38次方 到 +3.4 × 10的38;
double -> System.Double :双精度浮点型。
decimal->System.Decimal:占16个字节。
具体信息如下
可通过类似以下程序获取类型长度信息:
long l = 1000;
int long_length = Marshal.SizeOf(l);
输出类型信息
1 private static List<string> GetSimpleTypeNameList()
2 {
3 List<string> simpleTypeList = new List<string>();
4 simpleTypeList.Add(typeof(char).ToString());
5 simpleTypeList.Add(typeof(short).ToString());
6 simpleTypeList.Add(typeof(ushort).ToString());
7 simpleTypeList.Add(typeof(int).ToString());
8 simpleTypeList.Add(typeof(uint).ToString());
9 simpleTypeList.Add(typeof(long).ToString());
10 simpleTypeList.Add(typeof(ulong).ToString());
11 simpleTypeList.Add(typeof(float).ToString());
12 simpleTypeList.Add(typeof(double).ToString());
13 simpleTypeList.Add(typeof(decimal).ToString());
14 simpleTypeList.Add(typeof(bool).ToString());
15 simpleTypeList.Add(typeof(byte).ToString());
16 simpleTypeList.Add(typeof(sbyte).ToString());
17
18 simpleTypeList.Add(typeof(DateTime).ToString());
19 simpleTypeList.Add(typeof(object).ToString());
20 simpleTypeList.Add(typeof(string).ToString());
21 simpleTypeList.Add(typeof(Array).ToString());
22 simpleTypeList.Add(typeof(char?).ToString());
23 simpleTypeList.Add(typeof(short?).ToString());
24 simpleTypeList.Add(typeof(ushort?).ToString());
25 simpleTypeList.Add(typeof(int?).ToString());
26 simpleTypeList.Add(typeof(uint?).ToString());
27 simpleTypeList.Add(typeof(long?).ToString());
28 simpleTypeList.Add(typeof(ulong?).ToString());
29 simpleTypeList.Add(typeof(float?).ToString());
30 simpleTypeList.Add(typeof(double?).ToString());
31 simpleTypeList.Add(typeof(decimal?).ToString());
32 simpleTypeList.Add(typeof(bool?).ToString());
33 simpleTypeList.Add(typeof(DateTime?).ToString());
34 simpleTypeList.Add(typeof(byte?).ToString());
35 simpleTypeList.Add(typeof(sbyte?).ToString());
36
37 return simpleTypeList;
38 }
调用输出得到信息如下:
各类型的默认值
数值类型的默认值一般都是0;
时间类型默认0001/1/1 0:00:00;
其他类型默认为空。
示例如下
1 Console.WriteLine(default(int));
2 Console.WriteLine(default(float));
3 Console.WriteLine(default(DateTime));
4 Console.WriteLine(default(char));
5 Console.WriteLine(default(string));
6 Console.WriteLine(default(Array));
7 Console.WriteLine(default(int?));
输出结果:
以上
-------------------------------------
最新文章
- ASP.NET MVC 扩展数据验证 转
- UESTC 1817 Complete Building the Houses
- 第二周 PSP项目计划
- (转)Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
- 使用Netty进行Android与Server端通信实现文字发送接收与图片上传
- html5 API
- 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)
- Android Service命令
- 【Android笔记】MediaPlayer基本用法
- Node.js Buffer
- javascript字符串与数组转换汇总
- SuperMap iClient 查询成功后如何传递参数?
- iOS集合视图单元格高亮和选中的区别
- 责任链模式 职责链模式 Chain of Responsibility Pattern 行为型 设计模式(十七)
- 在Ubuntu/CentOS/Debian系统下,使用CPU挖Monero (XMR)币
- HBase之CF持久化系列(续2)
- hql- 使用like的小坑①
- Cobbler自动装机--2
- virtualbox+vagrant学习-4-Vagrantfile-6-SSH Settings
- 把 Reative Native 47 版本集成到已有的 Native iOS 工程中