CKTIS / C#
delegates and events: riferimento pratico
delegates and events: riferimento pratico: Un delegato conserva un contratto invocabile, mentre un event limita al proprietario la pubblicazione degli iscritti.
Versione: C# language and .NET 10 reference runtime
Sintassi
// delegates eventsUn delegato conserva un contratto invocabile, mentre un event limita al proprietario la pubblicazione degli iscritti.
Usa callback ed eventi per notifiche disaccoppiate con ciclo di vita esplicito.
Una sottoscrizione mantiene un riferimento al destinatario; omettere unsubscribe può causare una perdita.
L’eccezione di un handler interrompe una normale invocazione multicast.
Il programma completo usa realmente delegates and events; lo stdout esatto atteso è 5.
Esempi
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(5); sealed class Source { public event Action<int>? Changed; public void Raise(int value) => Changed?.Invoke(value); }
Risultato
5
Il programma completo usa realmente `delegates and events`; lo stdout esatto atteso è `5`.