Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / C#

set: praktická referencia

set: praktická referencia: set zapisuje property alebo indexer a priraďovanú hodnotu sprístupňuje ako value.

Verzia: C# language; verified against C# 14 reference

Syntax

set /* grammar-specific form */

set zapisuje property alebo indexer a priraďovanú hodnotu sprístupňuje ako value.

Použite ho na riadenú mutáciu s overením invariantu.

Setter môže vyhodiť výnimku či spôsobiť vedľajšie účinky, hoci priradenie vyzerá jednoducho.

set môže mať užšiu prístupnosť; init obmedzuje zápis na inicializáciu.

Úplný program skutočne používa set; presne očakávaný stdout je 4.

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 item = new Item(); item.Value = 4; Console.WriteLine(item.Value); sealed class Item { public int Value { get; set; } }

Výsledok

4

Úplný program skutočne používa `set`; presne očakávaný stdout je `4`.

Zdroje