Znalostní bázeCentrum zkoušek
CSČeština
Vybrat jazyk13

CKTIS / C#

records and equality: praktická reference

records and equality: praktická reference: Record poskytuje hodnotovou sémantiku a podporu with; Deconstruct automaticky syntetizuje pouze poziční record.

Verze: C# language and .NET 10 reference runtime

Syntaxe

// records

Record poskytuje hodnotovou sémantiku a podporu with; Deconstruct automaticky syntetizuje pouze poziční record.

Použijte pro data, jejichž identita vyplývá ze složek.

Neměnnost recordu je mělká; vlastnost může odkazovat na měnitelný objekt.

Rovnost zahrnuje runtime typ a with kopíruje reference složek.

Úplný program skutečně používá records and equality; přesně očekávaný stdout je Ada:2.

Příklady

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);

Výsledek

Ada:2

Úplný program skutečně používá `records and equality`; přesně očekávaný stdout je `Ada:2`.

Zdroje