CKTIS / C#
records and aggregates: odniesienie praktyczne
records and aggregates: odniesienie praktyczne: Agregaty grupują składowe w jedną wartość; tuple i rekord pozycyjny wspierają deconstruction, lecz zwykły record bez pozycyjnej listy parametrów nie generuje automatycznie Deconstruct.
Wersja: C# language and .NET 10 reference runtime
Składnia
// records aggregatesAgregaty grupują składowe w jedną wartość; tuple i rekord pozycyjny wspierają deconstruction, lecz zwykły record bez pozycyjnej listy parametrów nie generuje automatycznie Deconstruct.
Stosuj do jawnych wyników wielopolowych zamiast luźnych parametrów.
Duże mutable agregaty utrudniają utrzymanie invariantu.
Default może zawierać zera/null, a deconstruction musi zachować znaczenie kolejności.
Kompletny program rzeczywiście używa records and aggregates; oczekiwany stdout to dokładnie 5.
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 point = new Point(2, 3); var (x, y) = point; Console.WriteLine(x + y); readonly record struct Point(int X, int Y);
Wynik
5
Kompletny program rzeczywiście używa `records and aggregates`; oczekiwany stdout to dokładnie `5`.