聊一聊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?));
输出结果:

以上
-------------------------------------
聊一聊C#基本类型的更多相关文章
- 辛巴学院-Unity-剑英陪你零基础学c#系列(二)顺序
这不是草稿 辛巴学院:正大光明的不务正业. 上一次的教程写出来之后,反馈还是挺多的,有很多都做了修改,也有一些让人崩溃,不得不说上几句.有些人有些很奇怪的地方,你写篇东西,被看了以后不说他感觉怎么 ...
- 聊一聊 InnoDB 引擎中的索引类型
索引对数据库有多重要,我想大家都已经知道了吧,关于索引可能大家会对它多少有一些误解,首先索引是一种数据结构,并且索引不是越多越好.合理的索引可以提高存储引擎对数据的查询效率. 形象一点来说呢,索引跟书 ...
- 【跟着子迟品 underscore】常用类型判断以及一些有用的工具方法
Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...
- linux 文件权限、类型、命名规则
文件权限 -rwxr-x--t 文件类型 用户权限 组权限 其他用户权限 umask是一个掩码,设置文件的默认权限,会屏蔽掉不想授予该安全级别的权限,从对象的全权权限中减掉:对文件全权权 ...
- 【腾讯Bugly干货分享】聊一聊微信“小程序”
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57ecdf5ef03abecd43216fd0 Dev Club 是一个交流移动 ...
- 15天玩转redis —— 第六篇 有序集合类型
今天我们说一下Redis中最后一个数据类型 “有序集合类型”,回首之前学过的几个数据结构,不知道你会不会由衷感叹,开源的世界真好,写这 些代码的好心人真的要一生平安哈,不管我们想没想的到的东西,在这个 ...
- Underscore.js 常用类型判断以及一些有用的工具方法
1. 常用类型判断以及一些有用的工具方法 underscore.js 中一些 JavaScript 常用类型检查方法,以及一些工具类的判断方法. 首先我们先来谈一谈数组类型的判断.先贴出我自己封装好的 ...
- 细说Nullable<T>类型
目录一.简介二.语法和用法三.类型的转换和运算四.装箱与拆箱五.GetType()方法六.ToString()方法七.System.Nullable帮助类八.语法糖 一.简介 众所周知,值类型变量不能 ...
- ( 转 ) 聊一聊C#的Equals()和GetHashCode()方法
聊一聊C#的Equals()和GetHashCode()方法 博客创建一年多,还是第一次写博文,有什么不对的地方还请多多指教. 关于这次写的内容可以说是老生长谈,百度一搜一大堆.大神可自行绕路. ...
随机推荐
- windows 漏洞列表
漏洞列表 #Security Bulletin #KB #Description #Operating System CVE-2017-0213 [Windows COM Eleva ...
- hosts文件的内容
C:\Windows\System32\drivers\etc\hosts 1 # Copyright (c) 1993-2009 Microsoft Corp. 2 # 3 # This is a ...
- Spring的BeanFactory是什么?
什么是BeanFactory? 提到Spring,总是让人第一时间想起IOC容器,而IOC容器的顶层核心接口就是我们的BeanFactory,如果能够理解BeanFactory的体系结构想必能让我们对 ...
- Elasticsearch(3):别名
ES中可以为索引添加别名,一个别名可以指向到多个索引中,同时在添加别名时可以设置筛选条件,指向一个索引的部分数据,实现在关系数据库汇总的视图功能,这就是ES中别名的强大之处.别名是一个非常实用的功 ...
- iOS使用NSTextAttachment添加图片,图片模糊
最近在忙的项目中,需要处理富文本的相关内容,产品需求并不复杂,所以想着用TextKit处理,顺便学习一下,没想到直接掉坑.在此记录一下(都是血泪史),顺便为有需要的小伙伴提供参考. // Add th ...
- ubuntu19.10如何添加开机启动项
$sudo vi /lib/systemd/system/rc-local.service内容如下[Unit]Description=/etc/rc.local CompatibilityDocume ...
- http_parser
最近读了 http_parser 的源码,记录下. 有意思的地方: 1) 协议解析可以不完全解析完,但是当前 parser 会记录解析状态,这样可以继续解析 2) 协议解析首要还是要了解协议 ...
- Redis 客户端 Jedis、lettuce 和 Redisson 对比
Redis 支持多种语言的客户端,下面列举了部分 Redis 支持的客户端语言,大家可以通过官网查看 Redis 支持的客户端详情. C语言 C++ C# Java Python Node.js PH ...
- day08 Pyhton学习
一.昨日内容回顾 .1.基础部分的补充 join() 把列表变成字符串, 拼接 split() 切割 删除: 列表和字典不能在循环的时候进行删除. 把要删除的内容记录在一个新列表中,然后循环新列表, ...
- 【迷宫问题】CodeForces 1292A A NEKO's Maze Game
题目大意 vjudge链接 共两行,从(1,n)到(2,n). 每过一个时刻会有一个位置的状态变化,从能到达这个位置变成不能到达,或从不能到达变成能到达,问在每个时刻中是否能从起点到终点. 数据范围 ...