Repository navigation
ZH TW CSharp Structs Records
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
struct — 值型別; class 是引用型別。 record指定具有方便的資料相等語意的型別; record class和record struct是可能的。 with 使用修改後的成員建立記錄的副本,保留原始物件。
請將此範例另存為 Example.cs,並與本頁其他範例分開執行。
Example.cs:
// PROJECT-EXAMPLE
var original = new Item("ore", 1);
var copy = original with { Count = 3 };
System.Console.WriteLine($"{original.Count}:{copy.Count}");
record Item(string Name, int Count);結果:
1:3
record Position(int X, int Y);
var first = new Position(10, 20);
var second = first with { X = 11 };
UO.Print($"First={first}, second={second}");readonly record struct Point(int X, int Y);
var point = new Point(10, 20);
(int X, int Y) coordinates = (point.X, point.Y);
var (x, y) = coordinates;
UO.Print($"{x}, {y}");元組將多個值分組,無需單獨的類別聲明。它的元素名稱提高了可讀性,但沒有創建獨立的新類型。對於本機類型,區分引用相等、Equals和重載運算子==。
ClassicUO — Bad Newbie & C# Scripts IDE