;

C# Func Delegate


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.

Introduction to Func Delegate

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.

Key Points:

  • Generic Return Type: Func delegates always have a return type, defined as the last type parameter.
  • Parameter Flexibility: Func supports methods with 0 to 16 parameters, accommodating a range of function signatures.

Understanding Func Delegate Syntax

The Func delegate is defined generically:

Syntax

Func<in T1, in T2, ..., out TResult>

where,

    • T1, T2, ...: Input parameter types.
    • TResult: Return type of the function.

Example: Basic Func Declaration

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.
  • The add lambda expression performs addition on two integers and returns the result.

Examples of Using Func Delegate

Example 1: Func with a Single Parameter

Here, Func is used to calculate the square of a number.

Func<int, int> square = x => x * x;
Console.WriteLine(square(5)); // Output: 25

Explanation:

  • Func<int, int> specifies a delegate with one integer parameter and an integer return type.
  • The lambda expression x => x * x calculates the square of x.

Example 2: Func with Multiple Parameters

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

Explanation:

  • Func<int, int, int> indicates a delegate with two integers as input parameters and an integer return type.
  • The lambda expression (a, b) => a > b ? a : b returns the larger of two integers.

Example 3: Func with No Parameters

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

Explanation:

  • Func<int> specifies a delegate that takes no parameters and returns an integer.
  • The lambda () => new Random().Next(1, 100) generates and returns a random integer between 1 and 99.

Real-World Example: Data Transformation

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);
    }
}

Explanation:

  • 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.
  • Chaining in LINQ: Using LINQ’s Where method to filter and Select to transform, the delegates create a flexible pipeline for data processing.

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.

Key Takeaways

  • Func Simplifies Code: The Func delegate simplifies method definitions, allowing you to pass functions directly as parameters.
  • Versatile in Parameters: It can support functions with up to 16 parameters, making it versatile for different function signatures.
  • Ideal for LINQ and Data Processing: Func works seamlessly with LINQ, providing powerful data filtering and transformation capabilities.
  • Enhanced Code Reusability: Using Func delegates in combination with lambda expressions can make code more modular and reusable.

Summary

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#.