A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-func-delegate/ below:

C# Func Delegate - GeeksforGeeks

C# Func Delegate

Last Updated : 11 Jul, 2025

In C#, a delegate is a type that references a method. When creating a custom delegate, we follow these steps:

  1. Declare a delegate with a signature matching the target method.
  2. Create an instance of the delegate.
  3. Invoke the method using the delegate.

But defining custom delegates manually can be repetitive. To simplify this, C# provides a built-in delegate called Func<>, which reduces the need to declare custom delegates explicitly.

Example: Custom Delegate (Without Func)

C#
// C# Program to demonstrate a custom delegate
using System;  

class Geeks 
{  
    // Custom delegate declaration  
    public delegate int Delegate(int a, int b, int c, int d);  

    // Method to be assigned to the delegate  
    public static int Multiply(int a, int b, int c, int d)  
    {  
        return a * b * c * d;  
    }  

    static void Main()  
    {  
        // Using the custom delegate  
        Delegate o = Multiply;  
        Console.WriteLine(o(12, 34, 35, 34));  
    }  
}

Here, we manually define a delegate type "Delegate" that matches the method signature.

A Func delegate is a built-in generic delegate provided by C#. It simplifies delegate creation by eliminating the need for explicit delegate declarations.

Func Delegate Syntax:

// Func delegate with zero input parameters and one return type

public delegate TResult Func<out TResult>();

// Func delegate with one input parameter and one return type

public delegate TResult Func<in T, out TResult>(T arg);

// Func delegate with two input parameters and one return type

public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);

// Func delegate with sixteen input parameters and one return type

public delegate TResult Func<in T1, in T2, ..., in T16, out TResult>(

T1 arg1, T2 arg2, ..., T16 arg16);

Example 1: Using Func Delegate

C#
// C# Program to demonstrate Func delegate with a method
using System;  

class Geeks
{  
    // Method assigned to the Func delegate  
    public static int Multiply(int a, int b, int c, int d)  
    {  
        return a * b * c * d;  
    }  

    static void Main()  
    {  
        // Using Func delegate (four input parameters, one return type)  
        Func<int, int, int, int, int> Delegate = Multiply;  
        Console.WriteLine(Delegate(10, 100, 1000, 1));  
    }  
}

Example 2: Func Delegate with One Parameter

C#
// C# Program to demonstrate 
// Func delegate with a single parameter
using System;  

class Geeks
{  
    // Method assigned to the Func delegate  
    public static int DoubleValue(int num)  
    {  
        return num + num;  
    }  

    static void Main()  
    {  
        // Func delegate with one input and one return type  
        Func<int, int> Delegate = DoubleValue;  
        Console.WriteLine(Delegate(10));  
    }  
}
Func Delegate with Anonymous Method

Example:

C#
// C# Program to demonstrate Func delegate with an anonymous method
using System;

class Geeks
{
    static void Main()
    {
        // Using Func delegate with an anonymous method  
        Func<int, int, int> sum = delegate (int x, int y) 
        { 
            return x + y; 
        };  

        Console.WriteLine(sum(5, 10));  
    }
}
Func Delegate with Lambda Expression

Example:

C#
// C# Program to demonstrate 
// Func delegate with a lambda expression
using System;

class Geeks
{
    static void Main()
    {
        // Using Func delegate with a lambda expression  
        Func<int, int, int> sum = (x, y) => x + y;  
        Console.WriteLine(sum(5, 10));  
    }
}

Important Points:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4