The Func
delegate in C# provides a streamlined, flexible way to define and pass functions as parameters, particularly for functions that return values. This tutorial explains the Func delegate, demonstrates its uses, and explores real-world applications.
The Func
delegate in C# represents a method that can take up to 16 input parameters and returns a result. It simplifies passing functions as parameters, making it valuable in scenarios involving higher-order functions, functional programming patterns, and generic functions.
The Func
delegate is defined generically:
Func<in T1, in T2, ..., out TResult>
where,
T1
, T2
, ...: Input parameter types.TResult
: Return type of the function.Func<int, int, int> add = (x, y) => x + y;
Console.WriteLine(add(3, 4)); // Output: 7
In this example:
Func<int, int, int>
defines a delegate with two integer parameters (x
, y
) and an integer return type.Here, Func
is used to calculate the square of a number.
Func<int, int> square = x => x * x;
Console.WriteLine(square(5)); // Output: 25
Func<int, int>
specifies a delegate with one integer parameter and an integer return type.x => x * x
calculates the square of x
.In this example, we use Func
to find the maximum of two numbers.
Func<int, int, int> max = (a, b) => a > b ? a : b;
Console.WriteLine(max(10, 20)); // Output: 20
Func<int, int, int>
indicates a delegate with two integers as input parameters and an integer
return type.(a, b) => a > b ? a : b
returns the larger of two integers.Func
can be used to generate a random number without requiring any input.
Func<int> getRandomNumber = () => new Random().Next(1, 100);
Console.WriteLine(getRandomNumber()); // Output: Random number between 1 and 99
Func<int>
specifies a delegate that takes no parameters and returns an integer.lambda
() => new Random().Next(1, 100)
generates and returns a random integer between 1 and 99.Consider a real-world scenario where you need to filter and transform data in a list. Func is particularly useful for defining transformation and filtering logic that can be reused across different lists.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// List of integers
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Define Func to filter even numbers
Func<int, bool> isEven = x => x % 2 == 0;
// Define Func to double the number
Func<int, int> doubleNumber = x => x * 2;
// Apply filtering and transformation
var evenDoubledNumbers = numbers
.Where(isEven) // Filter even numbers
.Select(doubleNumber) // Double each even number
.ToList();
// Output: 4, 8, 12, 16, 20
evenDoubledNumbers.ForEach(Console.WriteLine);
}
}
isEven
: This Func<int, bool> is a delegate that takes an integer and returns a boolean indicating if the integer is even.doubleNumber
: This Func<int, int>
is a delegate that doubles the input integer.Real-World Use Case: This pattern is useful in data processing pipelines, such as those used in financial analysis, inventory management, or data science, where data needs to be filtered, transformed, and analyzed in specific ways.
Func
delegate simplifies method definitions, allowing you to pass functions directly as parameters.Func
works seamlessly with LINQ, providing powerful data filtering and transformation capabilities.Func
delegates in combination with lambda expressions can make code more modular and reusable.The Func
delegate in C# is a powerful tool for defining methods inline and passing them as parameters. It supports up to 16 input parameters and a return type, making it flexible and versatile for different functional programming needs. Commonly used in LINQ queries, Func
delegates streamline data processing pipelines and allow for modular, reusable code structures. Whether filtering, transforming, or generating data, Func offers a concise, powerful way to work with functions in C#.