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

推荐订阅源

S
Security @ Cisco Blogs
H
Hacker News: Front Page
P
Privacy International News Feed
N
News and Events Feed by Topic
T
Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
S
Schneier on Security
K
Kaspersky official blog
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
L
Lohrmann on Cybersecurity
Jina AI
Jina AI
P
Proofpoint News Feed
AI
AI
雷峰网
雷峰网
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 叶小钗
Webroot Blog
Webroot Blog
Apple Machine Learning Research
Apple Machine Learning Research
SecWiki News
SecWiki News
罗磊的独立博客
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
S
Security Affairs
Blog — PlanetScale
Blog — PlanetScale
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
月光博客
月光博客
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Securelist
W
WeLiveSecurity
T
Troy Hunt's Blog
A
Arctic Wolf
博客园 - 司徒正美

博客园 - 牛奶哥

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