ZH TW CSharp Operator Cast - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
明確轉換
請求明確轉換,特別是數字、引用轉換或自訂明確運算子。不可能的引用轉換會引發異常。
簡單範例 (Example.cs)
請將此範例另存為 Example.cs,並與本頁其他範例分開執行。
Example.cs:
// PROJECT-EXAMPLE
double value = 3.9;
System.Console.WriteLine((int)value);
結果:
3
句法
(TargetType)value
範例1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
double value = 12.75;
int whole = (int)value;
Console.WriteLine(whole);
}
}
範例2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
object value = "ore";
string name = (string)value;
Console.WriteLine(name.Length);
}
}
官方來源:Microsoft學習。