Baza znanjaIzpitno središče
SLSlovenščina
Izberi jezik13

CKTIS / C#

remove: praktična referenca

remove: praktična referenca: remove določa accessor za odjavo od event in prejme handler kot value.

Različica: C# language; verified against C# 14 reference

Sintaksa

remove /* grammar-specific form */

remove določa accessor za odjavo od event in prejme handler kot value.

Uporabite z add, ko shranjevanje naročnin zahteva lasten nadzor.

Nesimetrija med add in remove povzroči uhajanje ali neučinkovit unsubscribe.

Odstranitev manjkajočega handlerja mora imeti zavestno določeno vedenje.

Celoten program dejansko uporabi remove; natančno pričakovani stdout je done.

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 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

Celoten program dejansko uporabi `remove`; natančno pričakovani stdout je `done`.

Viri