Anonymous Methods
Anonymous methods enable developers to define method logic inline without creating a seperate named method. They are typically used in conjunction with delegates.
Syntax:
delegate void MessageHandler(string content);
MessageHandler handler = delegate(string text) {
Console.WriteLine(text);
};
handler("Message from anonymous method!");
Characteristics:
- Parameter types must be explicitly declared
- Useful for straightforward delegate scenarios but tends to produce verbose code
Lambda Expressions
Lambda expressions offer a more concise syntax for writing anonymous methods. Introduced in C# 3.0, they provide a cleaner approach to delegate-based programming.
Syntax:
// Type inference enabled
Action<string> conciseVersion = msg => Console.WriteLine(msg);
// Explicit type declaration
Action<string> verboseVersion = (string msg) => Console.WriteLine(msg);
conciseVersion("Lambda with inferred types!");
verboseVersion("Lambda with explicit types!");
Characteristics:
- Supports parameter type inference for reduced boilerplate
- Can represent single expressions or statement blocks
- Generally more readable than anonymous methods
LINQ (Language Integrated Query)
LINQ provides unified query capabilities across diverse data sources including collections, arrays, XML, and databases. It allows developers to write declarative code for data manipulation.
Example with Lambda Expressions:
using System;
using System.Collections.Generic;
using System.Linq;
class DataProcessor
{
static void Main()
{
List<int> dataset = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Filter even numbers, sort, and transform
var transformedEvens = dataset.Where(num => num % 2 == 0)
.OrderBy(num => num)
.Select(num => num * 2)
.ToList();
transformedEvens.ForEach(num => Console.WriteLine(num));
// Categorize and count elements by remainder
var categories = dataset.GroupBy(num => num % 3)
.Select(g => new { Remainder = g.Key, Total = g.Count() })
.ToList();
categories.ForEach(cat => Console.WriteLine($"Remainder: {cat.Remainder}, Count: {cat.Total}"));
}
}
Integration with Lambda Expressions:
The LINQ methods in the example above leverage lambda expressions for filtering (Where), ordering (OrderBy), projection (Select), and grouping (GroupBy) operations. These expressions serve as arguments to extension methods, enabling powerful declarative data processing.
Key Differences
| Feature | Anonymous Methods | Lambda Expressions | LINQ |
|---|---|---|---|
| Type Declaration | Required | Inferred or explicit | N/A |
| Code Verbosity | Higher | Lower | Declarative syntax |
| Purpose | Inline method definitions | Concise delegates | Data querying |
| Use with Delegates | Direct | Direct | Via extension methods |
Anonymous methods require explicit parameter types and result in more verbose code.
Lambda expressions support type inference and offer both single-expression and block-body forms, making them the preferred choice for most scenarios.
LINQ represents a library of extension methods that enable declarative data manipulation. While it often pairs with lambda expressions, it can also except named methods or anonymous delegates as predicates and selectors.