Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
GimmeJob
Sign in
Programming · C# · Chapter 07 / 10

Programming learning path

Delegates, events, lambdas & LINQ

A delegate is a strongly typed reference to one or more compatible methods. Action and Func<...> are common generic delegate types. Lambda expressions provide compact syntax for creating delegates.

Delegate and lambda

Func<int, bool> isFailure = status => status >= 400;

Console.WriteLine(isFailure(500)); // True

An event is built on a delegate but controls who can publish it. Outside code normally subscribes or unsubscribes, while the declaring type raises the event.

LINQ

LINQ provides composable operators such as Where, Select, OrderBy, Any, FirstOrDefault and GroupBy. Many operators use deferred execution: the query is defined now but does not actually enumerate the source until a consumer requests the results.

var statuses = new[] { 200, 500, 404, 201 };

var failures = statuses
    .Where(status => status >= 400)
    .OrderBy(status => status)
    .ToList();

ToList() materializes the query immediately. Without it, many LINQ pipelines remain deferred and can re-run when enumerated again.

Key points

  • Delegate = typed callable reference.
  • Lambda = concise syntax for behavior that can become a delegate or expression tree.
  • Event separates subscription from the right to publish.
  • LINQ composes sequence operations; deferred execution matters for correctness and performance.

Source registry

2 chapter references
Delegates

Delegate types, callbacks and event foundations

Microsoft Learn · Official documentation
Source ↗
Language Integrated Query (LINQ)

LINQ operators, sequence queries and deferred execution

Microsoft Learn · Official documentation
Source ↗