Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

records and equality: practical reference

records and equality: practical reference: A record provides value semantics and with support; only a positional record synthesizes Deconstruct automatically.

Version: C# language and .NET 10 reference runtime

Syntax

// records

A record provides value semantics and with support; only a positional record synthesizes Deconstruct automatically.

Use it for data whose identity follows from its components.

Record immutability is shallow; a property can reference a mutable object.

Equality includes the runtime type, and with copies component references.

The complete program actually uses records and equality; its exact expected stdout is Ada:2.

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 first = new Person("Ada", 1); var second = first with { Level = 2 }; Console.WriteLine($"{second.Name}:{second.Level}"); sealed record Person(string Name, int Level);

Result

Ada:2

The complete program actually uses `records and equality`; its exact expected stdout is `Ada:2`.

Sources