CKTIS / C#
records and equality: riferimento pratico
records and equality: riferimento pratico: Un record fornisce semantica di valore e supporto a with; solo un record posizionale sintetizza automaticamente Deconstruct.
Versione: C# language and .NET 10 reference runtime
Sintassi
// recordsUn record fornisce semantica di valore e supporto a with; solo un record posizionale sintetizza automaticamente Deconstruct.
Usalo per dati la cui identità deriva dai componenti.
L’immutabilità del record è superficiale; una proprietà può riferire un oggetto modificabile.
L’uguaglianza include il tipo runtime e with copia i riferimenti dei componenti.
Il programma completo usa realmente records and equality; lo stdout esatto atteso è Ada:2.
Esempi
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);
Risultato
Ada:2
Il programma completo usa realmente `records and equality`; lo stdout esatto atteso è `Ada:2`.