CKTIS / C#
remove: практическа справка
remove: практическа справка: remove определя accessor за отписване от event и получава handler като value.
Версия: C# language; verified against C# 14 reference
Синтаксис
remove /* grammar-specific form */remove определя accessor за отписване от event и получава handler като value.
Използвайте с add, когато съхранението на абонаменти изисква собствен контрол.
Асиметрия между add и remove причинява течове или неефективен unsubscribe.
Премахването на липсващ handler трябва да има съзнателно определено поведение.
Пълната програма действително използва remove; точният очакван stdout е done.
Примери
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(); }
Резултат
done
Пълната програма действително използва `remove`; точният очакван stdout е `done`.