Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Equivalent to Java Functional Interfaces
The C# equivalent to Java's functional interfaces is delegates. Delegates in C# provide the same functionality as functional interfaces in Java, allowing you to treat methods as first-class objects and use lambda expressions for concise code.
Java functional interfaces define a contract with a single abstract method, while C# delegates directly represent method signatures that can be assigned lambda expressions or method references.
Syntax
Following is the syntax for declaring a delegate in C# −
public delegate ReturnType DelegateName(ParameterType parameter);
Following is the syntax for assigning a lambda expression to a delegate −
DelegateName delegateInstance = (parameter) => expression;
Java Functional Interface Implementation
Here's how a functional interface works in Java −
@FunctionalInterface
public interface MyInterface {
void invoke();
}
public class Demo {
void method() {
MyInterface x = () -> MyFunc();
x.invoke();
}
void MyFunc() {
System.out.println("Method executed");
}
}
C# Delegate Equivalent
The same functionality implemented using C# delegates −
using System;
public delegate void MyDelegate();
public class Demo {
public void Method() {
MyDelegate x = () => MyFunc();
x();
}
public void MyFunc() {
Console.WriteLine("Method executed");
}
public static void Main() {
Demo demo = new Demo();
demo.Method();
}
}
The output of the above code is −
Method executed
Using Built-in Delegates
C# provides built-in delegates like Action and Func that eliminate the need to declare custom delegates in most cases −
using System;
public class Demo {
public void Method() {
Action myAction = () => Console.WriteLine("Using Action delegate");
myAction();
Func<int, int, int> add = (a, b) => a + b;
Console.WriteLine("Sum: " + add(5, 3));
}
public static void Main() {
Demo demo = new Demo();
demo.Method();
}
}
The output of the above code is −
Using Action delegate Sum: 8
Comparison
| Java Functional Interface | C# Delegate |
|---|---|
| Requires interface declaration with @FunctionalInterface | Direct delegate declaration or built-in Action/Func |
| invoke() method must be called explicitly | Delegate can be invoked directly with () |
| Single abstract method constraint | Represents method signature directly |
| Implemented via lambda expressions | Supports lambda expressions and method groups |
Advanced Example with Parameters
Here's an example showing delegates with parameters and return values −
using System;
public delegate string StringProcessor(string input);
public class TextProcessor {
public void ProcessText() {
StringProcessor toUpper = text => text.ToUpper();
StringProcessor addPrefix = text => "Processed: " + text;
string result1 = toUpper("hello world");
string result2 = addPrefix("data");
Console.WriteLine(result1);
Console.WriteLine(result2);
}
public static void Main() {
TextProcessor processor = new TextProcessor();
processor.ProcessText();
}
}
The output of the above code is −
HELLO WORLD Processed: data
Conclusion
C# delegates serve as the equivalent to Java's functional interfaces, providing a more direct approach to method references and lambda expressions. Built-in delegates like Action and Func cover most use cases, making C# delegates more convenient than Java's functional interface approach.
