惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Scott Helme
Scott Helme
S
Securelist
美团技术团队
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
W
WeLiveSecurity
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
AI
AI
L
Lohrmann on Cybersecurity
S
Security Affairs
Cloudbric
Cloudbric
SecWiki News
SecWiki News
爱范儿
爱范儿
雷峰网
雷峰网
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
N
News and Events Feed by Topic
I
InfoQ
S
Secure Thoughts
AWS News Blog
AWS News Blog
A
About on SuperTechFans
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
The Last Watchdog
The Last Watchdog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Check Point Blog
P
Palo Alto Networks Blog
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News
Latest news
Latest news
I
Intezer
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
D
Docker

博客园 - 牛奶哥

C#点滴 - 深拷贝与浅拷贝 C#点滴 - 抽象类与接口区别 C#点滴 - ?与?? C#点滴 - 值类型与引用类型 C#点滴 - 关于String C#点滴 – 内建基本类型 C#点滴 – CLR, CTS…等等基本概念 SharePoint 2010 学习资料 初学SilverLight - (3)简单布局 初学SilverLight - (2)第一个SilverLight程序 初学SilverLight - (1)开发环境准备 WCF热带鱼书学习手记 - Security (1) 概述 关于在SharePoint中管理大文件的一些想法 WCF热带鱼书学习手记 - Service Contract Overload WCF热带鱼书学习手记 - client coding WCF热带鱼书学习手记 - metadata WCF热带鱼书学习手记 - endpoint - 牛奶哥 WCF热带鱼书学习手记 - ABC WCF热带鱼书学习手记 - Host Type - 牛奶哥
C#点滴 - 类型转换
牛奶哥 · 2010-03-26 · via 博客园 - 牛奶哥

1. 宽化转换() – Widening Conversions

从数据范围较窄的类型转换到数据范围较宽的类型,一般情况下不会带来数据的损失。

类型

不损失数据的可以转化目标类型

Byte

UInt16, Int16, UInt32, Int32, UInt64, Int64, Single, Double, Decimal

SByte

Int16, Int32, Int64, Single, Double, Decimal

Int16

Int32, Int64, Single, Double, Decimal

UInt16

UInt32, Int32, UInt64, Int64, Single, Double, Decimal

Char

UInt16, UInt32, Int32, UInt64, Int64, Single, Double, Decimal

Int32

Int64, Double, Decimal

UInt32

Int64, Double, Decimal

Int64

Decimal

UInt64

Decimal

Single

Double

但是在从整形到浮点类型转换的工程中,可能会造成数据精度损失。

类型

可以转化目标类型

Int32

Single

UInt32

Single

Int64

Single, Double

UInt64

Single, Double

Decimal

Single, Double

2. 窄化转换 – Narrowing Conversions

基本上.NET是不支持窄化转换的,所有的隐式的窄化都会造成编译错误。如果真的需要进行窄化转换,必须要进行强制转化或者使用System.Convert类中的方法。

默认情况下,即使溢出发生,强制窄化不会跑出异常。为了捕捉在窄化转换发生的错误,经常使用checked关键字,如:

        checked {

            int i = 10000;

            short j = (short)i;

        } 

而如果使用了System.Convert进行强制窄化转换,在溢出时一定会抛出异常。下面是一些进行窄化转换是溢出抛出异常的情况:

类型

转化的目标类型

Byte

Sbyte

SByte

Byte, UInt16, UInt32, UInt64

Int16

Byte, SByte, UInt16

UInt16

Byte, SByte, Int16

Char

Byte, SByte, Int16, UInt16, UInt32

Int32

Byte, SByte, Int16, UInt16, Int32

UInt32

Byte, SByte, Int16, UInt16, Int32, UInt32, UInt64

Int64

Byte, SByte, Int16, UInt16, Int32, UInt32, Int64

UInt64

Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64

Single

Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64

Double

Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64