Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / C#

records and aggregates: referência prática

records and aggregates: referência prática: Agregados agrupam componentes num valor; tuples e records posicionais suportam deconstruction, enquanto um record não posicional não sintetiza Deconstruct automaticamente.

Versão: C# language and .NET 10 reference runtime

Sintaxe

// records aggregates

Agregados agrupam componentes num valor; tuples e records posicionais suportam deconstruction, enquanto um record não posicional não sintetiza Deconstruct automaticamente.

Usa-os para resultados explícitos com vários campos em vez de parâmetros soltos.

Agregados mutáveis grandes dificultam manter um invariante.

Default pode conter zeros ou null e deconstruction deve preservar o significado da ordem.

O programa completo usa realmente records and aggregates; o stdout exato esperado é 5.

Exemplos

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 point = new Point(2, 3); var (x, y) = point; Console.WriteLine(x + y); readonly record struct Point(int X, int Y);

Resultado

5

O programa completo usa realmente `records and aggregates`; o stdout exato esperado é `5`.

Fontes