Repository navigation
ZH TW CSharp Variables
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
變數必須在使用前聲明,並在讀取前賦予值。本地名稱在其區塊內可見;參數 - 方法內部。 const 指定編譯時值,readonly 限制建構子或宣告的欄位賦值。
請將此範例另存為 Example.cs,並與本頁其他範例分開執行。
Example.cs:
// PROJECT-EXAMPLE
int count = 2;
count += 3;
const int Limit = 10;
System.Console.WriteLine($"{count}/{Limit}");結果:
5/10
const int MaximumAttempts = 3;
int completed = 0;
for (int attempt = 0; attempt < MaximumAttempts; attempt++)
{
completed++;
}
UO.Print($"Completed: {completed}");靜態欄位屬於型別,在一次執行中由該型別的執行個體共用。每次執行 .csx、.cs 或 .csproj 都會隔離使用者型別及其靜態欄位;快取編譯結果不會讓這些欄位跨次保留。共用 .NET 與主程式程式庫的靜態資料仍為共用,請勿在其中儲存特定角色的資料。
sealed class Options
{
public readonly int Delay;
public Options(int delay) => Delay = delay;
}
var options = new Options(100);
await WaitAsync(options.Delay);ClassicUO — Bad Newbie & C# Scripts IDE