CKTIS / C#
remove: praktična referenca
remove: praktična referenca: remove definira accessor za odjavu s event i prima handler kao value.
Verzija: C# language; verified against C# 14 reference
Sintaksa
remove /* grammar-specific form */remove definira accessor za odjavu s event i prima handler kao value.
Koristite s add kada pohrana pretplata zahtijeva vlastitu kontrolu.
Asimetrija između add i remove uzrokuje curenje ili neučinkovit unsubscribe.
Uklanjanje nepostojećeg handlera treba imati svjesno definirano ponašanje.
Cijeli program doista koristi remove; točno očekivani stdout jest done.
Primjeri
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(); Action handler = () => Console.WriteLine("bad"); source.Changed += handler; source.Changed -= handler; source.Raise(); Console.WriteLine("done");
sealed class Source { private Action? changed; public event Action Changed { add => changed += value; remove => changed -= value; } public void Raise() => changed?.Invoke(); }
Rezultat
done
Cijeli program doista koristi `remove`; točno očekivani stdout jest `done`.