Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

record: practical reference

record: practical reference: record generates value equality, printing, and support for with; only a positional record synthesizes Deconstruct automatically.

Version: C# language; verified against C# 14 reference

Syntax

record /* grammar-specific form */

record generates value equality, printing, and support for with; only a positional record synthesizes Deconstruct automatically.

Use it for data models whose identity follows from their components.

Immutability is shallow; a mutable member can still change observable state.

record class has reference nature and record struct value nature; equality includes runtime type.

The complete program actually uses record; its exact expected stdout is Ada.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var person = new Person("Ada"); Console.WriteLine(person.Name);
sealed record Person(string Name);

Result

Ada

The complete program actually uses `record`; its exact expected stdout is `Ada`.

Sources