聊一聊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()方法 博客创建一年多,还是第一次写博文,有什么不对的地方还请多多指教. 关于这次写的内容可以说是老生长谈,百度一搜一大堆.大神可自行绕路. ...
随机推荐
- centos7修改ssh端口及添加ssh监听端口
ssh 修改默认端口 [root@node-1 ~]# vi /etc/ssh/sshd_config 修改port 为 5522 重启[root@node-1 ~]# systemctl resta ...
- 中部:执具 | R语言数据分析(北京邮电大学)自整理笔记
第5章工欲善其事.必先利其器 代码,是延伸我们思想最好的工具. 第6章基础编程--用别人的包和函数讲述自己的故事 6.1编程环境 1.R语言的三段论 大前提:计算机语言程序=算法+数据结构 小前提:R ...
- 【小白学PyTorch】21 Keras的API详解(上)卷积、激活、初始化、正则
[新闻]:机器学习炼丹术的粉丝的人工智能交流群已经建立,目前有目标检测.医学图像.时间序列等多个目标为技术学习的分群和水群唠嗑答疑解惑的总群,欢迎大家加炼丹兄为好友,加入炼丹协会.微信:cyx6450 ...
- C# Socket TCP发送图片与接收图片
如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! ! --------------------------- ...
- Linux Centos7 安装Docker-CE
先确保yum 是最新版本 执行: sudo yum update 添加docker源地址 sudo yum-config-manager --add-repo https://download.doc ...
- centos8平台使用slabtop监控slab内存的状态
一,slabtop 所属的包: [root@yjweb ~]# whereis slabtop slabtop: /usr/bin/slabtop /usr/share/man/man1/slabto ...
- nginx优化:worker_processes/worker_connections/worker_rlimit_nofile
一,优化nginx的worker进程数 1,worker_processes应设置为多少? worker_processes 4; 如何设置这个值: worker_processes默认值是1,一般要 ...
- python操作excel xlwt (转)
Python中xlrd和xlwt模块使用方法 阅读目录 安装 xlrd模块使用 xlwt模块 xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入. 回到顶部 安装 ? ...
- C# indexof和indexofany区别(转)
定位子串是指在一个字符串中寻找其中包含的子串或者某个字符.在String类中,常用的定位子串和字符的方法包括IndexOf/LastIndexOf及IndexOfAny/LastIndexOfAny, ...
- 市场清仓价格算法 python求矩阵不同行不同列元素和的最大值
问题描述 求矩阵不同行不同列元素和的最大值(最小值) 问题求解 1.通过scipy库求解 scipy.optimize库中的linear_sum_assignment方法可以求解 输入一个矩阵,参数m ...