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?));

输出结果:

以上

-------------------------------------

最新文章

  1. ASP.NET MVC 扩展数据验证 转
  2. UESTC 1817 Complete Building the Houses
  3. 第二周 PSP项目计划
  4. (转)Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
  5. 使用Netty进行Android与Server端通信实现文字发送接收与图片上传
  6. html5 API
  7. 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)
  8. Android Service命令
  9. 【Android笔记】MediaPlayer基本用法
  10. Node.js Buffer
  11. javascript字符串与数组转换汇总
  12. SuperMap iClient 查询成功后如何传递参数?
  13. iOS集合视图单元格高亮和选中的区别
  14. 责任链模式 职责链模式 Chain of Responsibility Pattern 行为型 设计模式(十七)
  15. 在Ubuntu/CentOS/Debian系统下,使用CPU挖Monero (XMR)币
  16. HBase之CF持久化系列(续2)
  17. hql- 使用like的小坑①
  18. Cobbler自动装机--2
  19. virtualbox+vagrant学习-4-Vagrantfile-6-SSH Settings
  20. 把 Reative Native 47 版本集成到已有的 Native iOS 工程中

热门文章

  1. 2.1 java语言概述
  2. Example Code for a TMP102 I2c Thermometer————Arduino
  3. JavaFX FileChooser文件选择器,缓存上一次打开的目录
  4. 南方IT比赛项目
  5. 利用 JS 脚本实现网页全自动秒杀抢购
  6. Python实现好友生日提醒
  7. ConcurrentHashMap源码解析,多线程扩容
  8. 最新最最最简单的Snagit傻瓜式破解教程(带下载地址)
  9. dubbo使用问题
  10. C语言必踩神坑,世上本没有坑,摔的人多了,也就有了坑!