Func, Action And Predicate Delegates In C#

Listen to this Post

Delegates serve as the foundation for callbacks in .NET. They are reference types that hold references to methods with compatible signatures. In simple terms, delegates allow us to pass methods as arguments to other methods, thereby enabling us to achieve dynamic invocation.

In .NET 3.5, some new generic delegates—Func\<T>, Action\<T>, and Predicate\<T>—were introduced. Using generic delegates, it is possible to define concise delegate types without explicitly declaring them. These delegates are defined in the System namespace.

  1. Func\<T> – Takes up to 16 input parameters and returns a value.
  2. Action\<T> – Takes one or more input parameters but does not return anything.
  3. Predicate\<T> – Represents a method that takes an input parameter and returns a Boolean value indicating whether the input satisfies a condition.

You Should Know:

1. Using Func\ in C#

Func<int, int, int> add = (a, b) => a + b; 
Console.WriteLine(add(5, 3)); // Output: 8 

2. Using Action\ for Void Methods

Action<string> greet = name => Console.WriteLine($"Hello, {name}!"); 
greet("Alice"); // Output: Hello, Alice! 

3. Predicate\ for Conditional Checks

Predicate<int> isEven = num => num % 2 == 0; 
Console.WriteLine(isEven(4)); // Output: True 

4. Combining Delegates (Multicast Delegates)

Action<string> log = message => Console.WriteLine($"Log: {message}"); 
Action<string> notify = message => Console.WriteLine($"Notify: {message}");

Action<string> multiAction = log + notify; 
multiAction("System Alert"); 
// Output: 
// Log: System Alert 
// Notify: System Alert 

5. Real-World Use Case (LINQ with Predicate)

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; 
var evenNumbers = numbers.FindAll(isEven); 
// evenNumbers = [2, 4] 

6. Async Delegates with Func

Func<Task<string>> fetchData = async () => 
{ 
await Task.Delay(1000); 
return "Data fetched!"; 
};

Console.WriteLine(await fetchData()); // Output: Data fetched! 

What Undercode Say:

Delegates in C# provide a powerful way to implement callbacks, event handling, and dynamic method invocation. By mastering Func, Action, and Predicate, developers can write more flexible and reusable code.

Related Linux/Windows Commands for Developers:

  • Compile C# Code (Linux):
    mcs Program.cs && mono Program.exe 
    
  • Check .NET Version:
    dotnet --version 
    
  • Run PowerShell Script (Windows):
    .\Script.ps1 
    
  • List Running .NET Processes (Linux/Windows):
    ps aux | grep dotnet # Linux 
    Get-Process -Name "dotnet" # Windows PowerShell 
    
  • Debug .NET App:
    dotnet run --debug 
    

Delegates are essential for event-driven programming, LINQ operations, and asynchronous workflows. Understanding them deeply enhances code modularity and performance.

Expected Output:

A well-structured C# program utilizing Func, Action, and Predicate delegates for dynamic method execution and conditional logic.

References:

Reported By: Adnan Maqbool – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image