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#基本类型的更多相关文章

  1. 辛巴学院-Unity-剑英陪你零基础学c#系列(二)顺序

    这不是草稿 辛巴学院:正大光明的不务正业.   上一次的教程写出来之后,反馈还是挺多的,有很多都做了修改,也有一些让人崩溃,不得不说上几句.有些人有些很奇怪的地方,你写篇东西,被看了以后不说他感觉怎么 ...

  2. 聊一聊 InnoDB 引擎中的索引类型

    索引对数据库有多重要,我想大家都已经知道了吧,关于索引可能大家会对它多少有一些误解,首先索引是一种数据结构,并且索引不是越多越好.合理的索引可以提高存储引擎对数据的查询效率. 形象一点来说呢,索引跟书 ...

  3. 【跟着子迟品 underscore】常用类型判断以及一些有用的工具方法

    Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...

  4. linux 文件权限、类型、命名规则

    文件权限 -rwxr-x--t        文件类型 用户权限 组权限 其他用户权限 umask是一个掩码,设置文件的默认权限,会屏蔽掉不想授予该安全级别的权限,从对象的全权权限中减掉:对文件全权权 ...

  5. 【腾讯Bugly干货分享】聊一聊微信“小程序”

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57ecdf5ef03abecd43216fd0 Dev Club 是一个交流移动 ...

  6. 15天玩转redis —— 第六篇 有序集合类型

    今天我们说一下Redis中最后一个数据类型 “有序集合类型”,回首之前学过的几个数据结构,不知道你会不会由衷感叹,开源的世界真好,写这 些代码的好心人真的要一生平安哈,不管我们想没想的到的东西,在这个 ...

  7. Underscore.js 常用类型判断以及一些有用的工具方法

    1. 常用类型判断以及一些有用的工具方法 underscore.js 中一些 JavaScript 常用类型检查方法,以及一些工具类的判断方法. 首先我们先来谈一谈数组类型的判断.先贴出我自己封装好的 ...

  8. 细说Nullable<T>类型

    目录一.简介二.语法和用法三.类型的转换和运算四.装箱与拆箱五.GetType()方法六.ToString()方法七.System.Nullable帮助类八.语法糖 一.简介 众所周知,值类型变量不能 ...

  9. ( 转 ) 聊一聊C#的Equals()和GetHashCode()方法

    聊一聊C#的Equals()和GetHashCode()方法   博客创建一年多,还是第一次写博文,有什么不对的地方还请多多指教. 关于这次写的内容可以说是老生长谈,百度一搜一大堆.大神可自行绕路. ...

随机推荐

  1. I2C总线的Arduino库函数

    I2C总线的Arduino库函数 I2C即Inter-Integrated Circuit串行总线的缩写,是PHILIPS公司推出的芯片间串行传输总线.它以1根串行数据线(SDA)和1根串行时钟线(S ...

  2. vue : 无法加载文件 C:\Users\Lenovo\AppData\Roaming\npm\vue.ps1,因为在此系统上禁止运行脚本。

    第一步:用管理员身份打开 第二步:执行:set-ExecutionPolicy RemoteSigned 选择Y或A,回车

  3. linux配置定时任务cron/定时服务与自启动

    实现linux定时任务有:cron.anacron.at,使用最多的是cron任务 名词解释 cron--服务名:crond--linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与 ...

  4. sessionFactory' defined in class path /mappingDirectoryLocations配置问题

    问题:配置好aplicationContext.xml,启动tomcat 出现如下问题 sessionFactory无法正常建立 Context initialization failed org.s ...

  5. 浅谈Python常用英文单词

    一.交互式环境与print输出 1.print:打印/输出2.coding:编码3.syntax:语法4.error:错误5.invalid:无效6.identifier:名称/标识符7.charac ...

  6. FY2E HDF格式数据处理绘图

    圆盘标称投影数据时静止气象卫星常见的数据产品,比如FY2E静止气象卫星就有很多这样的产品(可以从国家卫星气象中心网站上下载).所谓的圆盘标称投影就是Geostationary投影,主要的投影参数有中央 ...

  7. 分布式锁结合SpringCache

    1.高并发缓存失效问题: 缓存穿透: 指查询一个一定不存在的数据,由于缓存不命中导致去查询数据库,但数据库也无此记录,我们没有将此次查询的null写入缓存,导致这个不存在的数据每次请求都要到存储层进行 ...

  8. 最大子段和之M子段和

    最大M子段和 题目模型 N个整数组成的序列 \(a_1,a_2,a_3,-,a_n\) ,将这N个数划分为互不相交的M个子段,并且这M个子段的和是最大的. 问题分析 方法一: 看到序列,我们首先要尝试 ...

  9. vue任意关系组件通信与跨组件监听状态 vue-communication

    大家好!我是木瓜太香! 众所周知,组件式开发方式给我们带来了方便,不过也引入了新的问题,组件之间的数据就像被一道无形的墙隔开,如果我们希望临时让两个组件直接通信,vuex 太巨,而 $emit 又不好 ...

  10. Spring Boot使用Mybatis实现增删改查

    java.com.wms.model.Admin.java 1 package com.wms.model; 2 3 import java.sql.Timestamp; 4 5 public cla ...