Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / C#

record: odniesienie praktyczne

record: odniesienie praktyczne: record generuje wartościową równość, drukowanie i wsparcie with; tylko rekord pozycyjny automatycznie generuje Deconstruct.

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

Składnia

record /* grammar-specific form */

record generuje wartościową równość, drukowanie i wsparcie with; tylko rekord pozycyjny automatycznie generuje Deconstruct.

Stosuj do modeli danych o tożsamości wynikającej ze składowych.

Niezmienność jest płytka; mutable member nadal zmienia obserwowany stan.

Record class ma referencyjną naturę, record struct wartościową; równość uwzględnia runtime type.

Kompletny program rzeczywiście używa record; oczekiwany stdout to dokładnie Ada.

Przykłady

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

Wynik

Ada

Kompletny program rzeczywiście używa `record`; oczekiwany stdout to dokładnie `Ada`.

Źródła