Delegates, Func, Action, Predicate in C# Explained with Examples & Interview Questions
Think of Delegates as function containers. They allow you to treat methods like variables — you can pass them around, store them, and execute them whenever you want.
🧩 What are Delegates in C#?
Read the Interview Question from Here.
A Delegate is like a pointer to a method.
It defines a type that holds a reference to a method with a particular signature (parameters + return type).
👉 Key points:
Delegates are type-safe (they check method signatures).
They allow methods to be passed as parameters.
They are used in event handling, LINQ, and callbacks.
🔹 Example: Basic Delegate
using System;
public delegate void GreetDelegate(string name);
class Program
{
static void SayHello(string name)
{
Console.WriteLine($”Hello, {name}!”);
}
static void SayGoodbye(string name)
{
Console.WriteLine($”Goodbye, {name}!”);
}
static void Main()
{
GreetDelegate greet = SayHello;
greet(”Shreya”);
greet = SayGoodbye;
greet(”Shreya”);
}
}
✅ Output:
Hello, Shreya!
Goodbye, Shreya!
🧩 Func in C#
Func is a built-in delegate type in C#.
It can take 0 to 16 input parameters.
It must return a value.
The last parameter is always the return type.
🔹 Example: Func
using System;
class Program
{
static int Add(int a, int b) => a + b;
static void Main()
{
Func<int, int, int> addFunc = Add;
int result = addFunc(5, 3);
Console.WriteLine($”Result: {result}”);
}
}
✅ Output:
Result: 8
🧩 Action in C#
Action is another built-in delegate.
It can take 0 to 16 parameters.
It does not return anything (void).
🔹 Example: Action
using System;
class Program
{
static void PrintMessage(string message)
=> Console.WriteLine($”Message: {message}”);
static void Main()
{
Action<string> printAction = PrintMessage;
printAction(”Welcome to Logic Lense!”);
}
}
✅ Output:
Message: Welcome to Logic Lense!
🧩 Predicate in C#
A predicate is a special built-in delegate.
It always takes one input parameter.
It always returns a bool (true/false).
🔹 Example: Predicate
using System;
using System.Collections.Generic;
class Program
{
static bool IsEven(int number) => number % 2 == 0;
static void Main()
{
Predicate<int> isEvenPredicate = IsEven;
Console.WriteLine(isEvenPredicate(10)); // True
Console.WriteLine(isEvenPredicate(7)); // False
}
}
✅ Output:
True
False
🔄 Quick Comparison
🎯 Real-Life Example
Imagine you’re filtering a list of numbers:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new() { 1, 2, 3, 4, 5, 6 };
Predicate<int> isEven = n => n % 2 == 0;
List<int> evenNumbers = numbers.FindAll(isEven);
Console.WriteLine(”Even Numbers:”);
evenNumbers.ForEach(Console.WriteLine);
}
}
✅ Output:
Even Numbers:
2
4
6
💡 10 Interview Questions on Delegates, Func, Action, Predicate
Read the Answers to this Interview Question from Here.
What is a delegate in C#, and why is it used?
Explain the difference between a delegate and an event in C#.
How does Func differ from Action in C#?
When would you use Predicate over Func?
Can a delegate point to multiple methods? Demonstrate with code.
What is a multicast delegate? Give an example.
What are the advantages of using built-in delegates (Func, Action, Predicate)?
Write a code example where you use Func for a calculation.
How can delegates improve testability in C# applications?
Explain covariance and contravariance in delegates with an example.
🏁 Conclusion
Delegates, Func, Action, and Predicate are powerful tools in C# that make code more flexible and reusable.
Delegates → General method references.
Func → Methods that return a value.
Action → Methods that return nothing.
Predicate → Methods that return true/false.
Mastering these will not only help in interviews but also in real-world C# projects. 🚀



Great explanation👍👍👍