CKTIS / C#
readonly: praktična referenca
readonly: praktična referenca: readonly prepreči ponovno dodelitev polja po gradnji ali označi nemutirajoči struct oziroma member.
Različica: C# language; verified against C# 14 reference
Sintaksa
readonly /* grammar-specific form */readonly prepreči ponovno dodelitev polja po gradnji ali označi nemutirajoči struct oziroma member.
Uporabite za ohranitev referenc in manj defensive copies v readonly struct.
Referenca readonly ne zamrzne objekta, na katerega kaže.
Polje se sme dodeliti v konstruktorju; readonly member ne sme spreminjati this.
Celoten program dejansko uporabi readonly; natančno pričakovani stdout je 2.
Primeri
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);
Rezultat
2
Celoten program dejansko uporabi `readonly`; natančno pričakovani stdout je `2`.