CKTIS / C#
readonly: praktická referencia
readonly: praktická referencia: readonly bráni novému priradeniu poľa po konštrukcii alebo označuje nemeniaci struct či member.
Verzia: C# language; verified against C# 14 reference
Syntax
readonly /* grammar-specific form */readonly bráni novému priradeniu poľa po konštrukcii alebo označuje nemeniaci struct či member.
Použite na zachovanie referencií a obmedzenie defensive copies v readonly struct.
Referencia readonly nezmrazí objekt, na ktorý ukazuje.
Pole možno priradiť v konštruktore; readonly member nesmie meniť this.
Úplný program skutočne používa readonly; presne očakávaný stdout je 2.
Príklady
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); Console.WriteLine(point.X);
readonly record struct Point(int X);
Výsledok
2
Úplný program skutočne používa `readonly`; presne očakávaný stdout je `2`.