Repository navigation
EN CSharp Structs Records
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
struct — value type; class is a reference type. record specifies a type with convenient data equality semantics; record class and record struct are possible. with creates a copy of the record with the modified members, preserving the original object.
Save this example separately as Example.cs. Run it independently of the other examples on this page.
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);Result:
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}");A tuple groups multiple values without a separate class declaration. Its element names improve readability, but do not create an independent new type. For native types, distinguish between reference equality, Equals, and the overloaded operator ==.
Official documentation: Structures, Records, Tuples and Comparison - Microsoft Learn.
ClassicUO — Bad Newbie & C# Scripts IDE