C#數(shù)值轉(zhuǎn)換-隱式數(shù)值轉(zhuǎn)換表參考
Implicit Conversion
隱式轉(zhuǎn)換就是直接使用,比如可以把一個 byte 類型直接用在 int 上。
比如以下直接把 byte 的 b 賦給 int 的 n,之間是直接用的,沒存在什么額外的關(guān)鍵字,全由系統(tǒng)自動完成類型轉(zhuǎn)換。
byte b = 1;
int n = b;
隱式數(shù)值轉(zhuǎn)換表(摘自 MSDN)
|
從 |
到 |
|---|---|
|
sbyte |
short、int、long、float、double 或 decimal |
|
byte |
short、ushort、int、uint、long、ulong、float、double 或 decimal |
|
short |
int、long、float、double 或 decimal |
|
ushort |
int、uint、long、ulong、float、double 或 decimal |
|
int |
long、float、double 或 decimal |
|
uint |
long、ulong、float、double 或 decimal |
|
long |
float、double 或 decimal |
|
char |
ushort、int、uint、long、ulong、float、double 或 decimal |
|
float |
double |
|
ulong |
float、double 或 decimal |
備注(摘自 MSDN)
從 int、uint 或 long 到 float 的轉(zhuǎn)換以及從 long 到 double 的轉(zhuǎn)換的精度可能會降低,但數(shù)值大小不受影響。
不存在到 char 類型的隱式轉(zhuǎn)換。
不存在浮點型與 decimal 類型之間的隱式轉(zhuǎn)換。
int 類型的常數(shù)表達式可轉(zhuǎn)換為 sbyte、byte、short、ushort、uint 或 ulong,前提是常數(shù)表達式的值處于目標(biāo)類型的范圍之內(nèi)。
相關(guān)文章
C#實現(xiàn)讓ListBox適應(yīng)最大Item寬度的方法
這篇文章主要介紹了C#實現(xiàn)讓ListBox適應(yīng)最大Item寬度的方法,涉及ListBox控件的操作技巧,需要的朋友可以參考下2015-05-05
C#中如何在Excel工作表創(chuàng)建混合型圖表實例
本篇文章主要介紹了C#中如何在Excel工作表創(chuàng)建混合型圖表實例,具有一定的參考價值,有需要的可以了解一下。2016-11-11

