CKTIS / C#
delegates and events: practical reference
delegates and events: practical reference: A delegate stores a callable contract, while an event restricts publishing the subscriber list to its owner.
Version: C# language and .NET 10 reference runtime
Syntax
// delegates eventsA delegate stores a callable contract, while an event restricts publishing the subscriber list to its owner.
Use callbacks and events for loose notification with an explicit lifecycle.
A subscription retains a reference to the receiver; failure to unsubscribe can leak it.
An exception from one handler interrupts ordinary multicast invocation.
The complete program actually uses delegates and events; its exact expected stdout is 5.
Examples
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); }
Result
5
The complete program actually uses `delegates and events`; its exact expected stdout is `5`.