Repository navigation
ZH TW CSharp Keyword checked
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
在其詞法上下文中啟用整數算術和相應轉換的溢出檢查。
請將此範例另存為 Example.cs,並與本頁其他範例分開執行。
Example.cs:
// PROJECT-EXAMPLE
int value = int.MaxValue;
try { System.Console.WriteLine(checked(value + 1)); }
catch (System.OverflowException) { System.Console.WriteLine("Overflow"); }結果:
Overflow
checked(expression) / checked { ... }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)
{
int maximum = int.MaxValue;
try { Console.WriteLine(checked(maximum + 1)); } catch (OverflowException) { Console.WriteLine("Overflow"); }
}
}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)
{
int maximum = int.MaxValue;
int wrapped = unchecked(maximum + 1);
Console.WriteLine(wrapped);
}
}官方來源:Microsoft學習。
ClassicUO — Bad Newbie & C# Scripts IDE