Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / C#

records and equality: referencia práctica

records and equality: referencia práctica: Un record proporciona semántica de valor y compatibilidad con with; solo un record posicional sintetiza Deconstruct automáticamente.

Versión: C# language and .NET 10 reference runtime

Sintaxis

// records

Un record proporciona semántica de valor y compatibilidad con with; solo un record posicional sintetiza Deconstruct automáticamente.

Úsalo para datos cuya identidad procede de sus componentes.

La inmutabilidad del record es superficial; una propiedad puede referenciar un objeto mutable.

La igualdad incluye el tipo runtime y with copia referencias de componentes.

El programa completo usa realmente records and equality; su stdout esperado exacto es Ada:2.

Ejemplos

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

Resultado

Ada:2

El programa completo usa realmente `records and equality`; su stdout esperado exacto es `Ada:2`.

Fuentes