CKTIS / C#
add: odniesienie praktyczne
add: odniesienie praktyczne: add definiuje niestandardowy akcesor subskrypcji zdarzenia i otrzymuje delegat jako value.
Wersja: C# language and .NET 10 reference runtime
Składnia
add /* grammar-specific form */add definiuje niestandardowy akcesor subskrypcji zdarzenia i otrzymuje delegat jako value.
Stosuj w event, gdy trzeba kontrolować przechowywanie handlerów.
Akcesory add/remove muszą być parą i zachować symetrię.
Współbieżne subskrypcje wymagają atomowego lub blokowanego zarządzania listą.
Kompletny program rzeczywiście używa add; oczekiwany stdout to dokładnie 3.
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 source = new Source(); source.Changed += value => Console.WriteLine(value); source.Raise(3);
sealed class Source { private Action<int>? changed; public event Action<int> Changed { add => changed += value; remove => changed -= value; } public void Raise(int value) => changed?.Invoke(value); }
Wynik
3
Kompletny program rzeczywiście używa `add`; oczekiwany stdout to dokładnie `3`.