A delegate in C# is a type-safe, object-oriented reference type that encapsulates a reference to a method with a specific signature and return type. Unlike traditional C++ function pointers, delegates are fully object-oriented, strictly type-checked by the compiler, and encapsulate both a method and, in the case of instance methods, the object instance on which the method is invoked. Under the hood, every delegate you define is compiled into a class that implicitly derives fromDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
System.MulticastDelegate, which itself inherits from System.Delegate. Because they are reference types, delegates can be assigned to variables, passed as arguments, and returned from methods.
Syntax and Mechanics
Working with a delegate involves three distinct phases: declaration, instantiation, and invocation. When instantiating a delegate, modern C# provides three primary mechanisms to bind a method to the delegate instance:- Method Group Conversion: Directly assigning an existing named method.
- Anonymous Methods: Defining an inline, unnamed method using the
delegatekeyword. - Lambda Expressions: A concise syntax (
=>) for creating anonymous functions, where the compiler infers parameter types and return types based on the delegate’s signature.
Nullability and Safe Invocation
Because delegates are reference types, their default value isnull. Attempting to invoke a null delegate directly throws a NullReferenceException. To prevent this, modern C# utilizes the null-conditional operator (?.) combined with the delegate’s explicit Invoke method.
For delegates with a void return type, this null-conditional invocation evaluates to void (producing no value). It simply short-circuits and skips the execution entirely as a statement.
Multicasting and Invocation Lists
Because C# delegates derive fromSystem.MulticastDelegate, a single delegate instance can hold an array of method references, known as an invocation list.
Methods are added to or removed from the invocation list using the += and -= operators (which compile down to Delegate.Combine and Delegate.Remove). When a multicast delegate is invoked, it executes the methods in its invocation list synchronously, in the exact order they were added.
The Target and Method Properties
A delegate instance tracks its underlying method via two primary properties inherited from System.Delegate:
Method: ASystem.Reflection.MethodInfoobject representing the signature and metadata of the targeted method.Target: Anobjectreference.- If the delegate points to an instance method,
Targetholds a reference to the specific class instance the method belongs to. - If the delegate points to a static method,
Targetevaluates tonull.
- If the delegate points to an instance method,
Built-in Generic Delegates
To eliminate the need for declaring custom delegate types for every unique signature, the .NET framework provides three highly generic, built-in delegate families in theSystem namespace:
ActionandAction<T...>: Encapsulates a method that returnsvoid. The non-genericSystem.Actiontakes exactly zero parameters, while the generic versions take from 1 up to 16 parameters.Func<TResult>andFunc<T..., TResult>: Encapsulates a method that returns a value of typeTResult. The zero-parameterSystem.Func<TResult>takes no arguments, while the generic versions take from 1 up to 16 parameters. The last type parameter always represents the return type.Predicate<T>: Encapsulates a method that takes exactly one parameter and returns abool.
Master C# with Deep Grasping Methodology!Learn More





