**顯式類型轉換**\- 顯式類型轉換,即**強制類型轉換**。顯式轉換需要強制轉換運算符,而且強制轉換會造成數據丟失。
下表顯示沒有[隱式轉換](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/implicit-numeric-conversions-table)的 .NET 數值類型之間的預定義顯式轉換。
| From | 到 |
| --- | --- |
| [sbyte](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | `byte`、`ushort`、`uint`、`ulong`或`char` |
| [byte](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | `sbyte`或`char` |
| [short](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | `sbyte`、`byte`、`ushort`、`uint`、`ulong`或`char` |
| [ushort](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | `sbyte`、`byte`、`short`或`char` |
| [int](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | `sbyte`、`byte`、`short`、`ushort`、`uint`、`ulong`或`char` |
| [uint](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | `sbyte`、`byte`、`short`、`ushort`、`int`或`char` |
| [long](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | `sbyte`、`byte`、`short`、`ushort`、`int`、`uint`、`ulong`或`char` |
| [ulong](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | `sbyte`、`byte`、`short`、`ushort`、`int`、`uint`、`long`或`char` |
| [char](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/char) | `sbyte`、`byte`或`short` |
| [float](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types) | `sbyte`、`byte`、`short`、`ushort`、`int`、`uint`、`long`、`ulong`、`char`或`decimal` |
| [double](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types) | `sbyte`、`byte`、`short`、`ushort`、`int`、`uint`、`long`、`ulong`、`char`、`float`或`decimal` |
| [decimal](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types) | `sbyte`、`byte`、`short`、`ushort`、`int`、`uint`、`long`、`ulong`、`char`、`float`或`double` |
例子:
```
double x=198410927.0112;
int y=(int)x; //結果 198410927
```
## 備注
* 顯式數值轉換可能會導致精度降低或導致引發異常,通常為[OverflowException](https://docs.microsoft.com/zh-cn/dotnet/api/system.overflowexception)。
* 將整數類型的值轉換為另一個整數類型時,結果取決于溢出[檢查上下文](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/checked-and-unchecked)。在已檢查的上下文中,如果源值在目標類型的范圍內,則轉換成功。否則會引發[OverflowException](https://docs.microsoft.com/zh-cn/dotnet/api/system.overflowexception)。在未檢查的上下文中,轉換始終成功,并按如下方式進行:
* 如果源類型大于目標類型,則通過放棄其“額外”最高有效位來截斷源值。結果會被視為目標類型的值。
* 如果源類型小于目標類型,則源值是符號擴展或零擴展,以使其與目標類型的大小相同。如果源類型帶符號,則是符號擴展;如果源類型是無符號的,則是零擴展。結果會被視為目標類型的值。
* 如果源類型與目標類型的大小相同,則源值將被視為目標類型的值。
* 將`decimal`值轉換為整型類型時,此值會向零舍入到最接近的整數值。如果生成的整數值處于目標類型的范圍之外,則會引發[OverflowException](https://docs.microsoft.com/zh-cn/dotnet/api/system.overflowexception)。
* 將`double`或`float`值轉換為整型類型時,此值會向零舍入到最接近的整數值。如果生成的整數值處于目標類型范圍之外,則結果會取決于溢出[上下文](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/checked-and-unchecked)。在已檢查的上下文中,引發[OverflowException](https://docs.microsoft.com/zh-cn/dotnet/api/system.overflowexception);而在未檢查的上下文中,結果是目標類型的未指定值。
* 將`double`轉換為`float`時,`double`值舍入為最接近的`float`值。如果`double`值太小或太大,無法匹配目標類型,結果將為零或無窮大。
* 將`float`或`double`轉換為`decimal`時,源值轉換為`decimal`表示形式,并并五入到第 28 位小數后最接近的數(如果需要)。根據源值的值,可能出現以下結果之一:
* 如果源值太小,無法表示為`decimal`,結果則為零。
* 如果源值為 NaN(非數值)、無窮大或太大而無法表示為`decimal`,則引發[OverflowException](https://docs.microsoft.com/zh-cn/dotnet/api/system.overflowexception)。
* 將`decimal`轉換為`float`或`double`時,`decimal`值舍入到最接近的`double`或`float`值。
- C#
- 基礎 System
- 命名規范
- 變量
- 數據類型
- 值類型
- 簡單類型
- 整數類型
- 字符類型
- 浮點類型
- 布爾類型
- 枚舉類型
- 結構體類型
- 引用類型
- 類類型
- 對象(Object)類型
- 字符串(String)類型
- 方法屬性
- 動態(Dynamic)類型
- 數組類型
- 接口Interface
- 委托類型delegate
- 裝箱和拆箱
- 指針類型
- 值類型與引用類型的區別
- Var類型
- 類型轉換
- 隱式轉換
- 顯式轉換
- 常量
- 常用函數
- 流程控制
- 循環
- 跳轉語句
- 數組
- 數組查找
- 添加組元素
- 復制與克隆數組
- 刪除數組元素
- 數組排序與反轉
- 數組排序
- 冒泡排序
- 直接插入排序
- 選擇排序法
- 數組對象
- 哈希表
- 其他
- 遞歸
- 屬性與方法及方法重載
- 結構與類
- 類
- 構造函數與析構函數
- 繼承
- 多態
- 泛型
- demo
- 漢字字符(串)比較
- 創建指定大小文件與讀取大文件
- 小截屏軟件功能
- 子窗體返回主窗體
- 判斷是否為數字
- 獲得字符串實際長度(包括中文字符)
- unity
- script腳本
- 腳本的生命周期
- 調試
- Unity Manual5.4
- Unity手冊
- Working In Unity
- Unity 2D
- Graphics(圖形)
- Physics(物理系統)
- Scripting(腳本)
- Multiplayer and Networking(多玩家和聯網)
- Audio(音頻)
- Animation(動畫)
- UI
- Navigation and Pathfinding(導航和尋路)
- Unity Services(Unity服務)
- Virtual Reality(虛擬現實VR)
- Open-source repositories(開源代碼庫)
- Platform-specific(特定于平臺的信息)
- Legacy Topics(舊版主題)
- Expert Guides(專家指南)
- 重要的類
- Object
- GameObject(重要)
- Component
- Transform(重要)
- Rigidbody
- ParticleSystem
- Behaviour
- Camera
- Animator
- AudioSource
- Animation
- AudioListener
- Light
- MonoBehaviour事件行為(重要)
- Terrain
- Collider
- Renderer
- Texture
- Mesh
- material
- Time
- Prefab預制件
- 功能demo
- 層級未知查找子物體
- 查找hp最小的敵人
- 查找最近的敵人
- 小項目之英雄無敵
- 界面操作
- Scene窗口
- Game窗口
- project窗口
- Hierarchy窗口
- 動畫
- Animation重要API
- 2D
- Sprite(creator) Sprite Renderer
- Sprite Editor
- Sprite Packer(遺留功能)
- 排序組組件
- 切片精靈
- 精靈遮罩
- 精靈圖集
- Tilemap
- 2D物理系統
- 全局設置(Project setting)
- 2D剛體(Rigidbody 2D)
- 碰撞(事件)消息
- 2D碰撞體(Collider 2D)
- 2D圓形碰撞體(Circle Collider 2D)
- 2D盒型碰撞體(BoxCollider 2D)
- 2D多邊形碰撞體( Polygon Collider 2D)
- 2D邊界碰撞體(EdgeCollider 2D)
- 2D膠囊碰撞體(CapsuleCollider 2D)
- 2D復合碰撞體(Composite Collider 2D)
- 2D物理材質(摩擦和彈性)(Physics Material 2D)
- 2D關節
- 2D距離關節(Distance Joint 2D)
- 2D固定關節(Fixed Joint 2D)
- 2D摩擦關節(Friction Joint 2D)
- 2D鉸鏈關節(Hinge Joint 2D)
- 2D相對關節(Relative Joint 2D)
- 2D滑動關節(Slider Joint 2D)
- 2D彈簧關節(Spring Joint 2D)
- 2D目標關節(Target Joint 2D)
- 2D車輪關節(Wheel Joint 2D)
- 2D恒定力(Constant Force 2D)
- 2D 效應
- 2D區域效應(AreaEffector 2D)
- 2D浮力效應(BuoyancyEffector 2D)
- 2D點效應(PointEffector 2D)
- 2D平臺效應(PlatformEffector 2D)
- 2D表面效應(SurfaceEffector 2D)
- 精靈動畫
- 2D動畫事件
- 重要類
- rigidbody2d
- 小竅門
- Unity中如何查找腳本掛載在哪個物體上