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
Func generic type in C#
The Func generic type in C# is a built-in delegate that represents a method which takes zero or more input parameters and returns a value. It provides a convenient way to store anonymous methods, lambda expressions, and regular methods that return a value.
Syntax
Following is the syntax for declaring a Func delegate −
Func<TResult> funcName; // No parameters, returns TResult Func<T, TResult> funcName; // One parameter T, returns TResult Func<T1, T2, TResult> funcName; // Two parameters, returns TResult Func<T1, T2, T3, ..., TResult> funcName; // Up to 16 parameters, returns TResult
The last type parameter is always the return type, while the preceding parameters are input types −
Func<int, string> converter = x => x.ToString();
Using Func with Different Parameter Types
Example
using System;
class Program {
static void Main(string[] args) {
// first type receives int and returns string
Func<int, string> one = (p) => string.Format("{0}", p);
// second type receives bool & long and returns string
Func<bool, long, string> two = (q, p) => string.Format("{0} and {1}", q, p);
// third type receives bool & int and returns string
Func<bool, int, string> three = (q, p) => string.Format("{0} and {1}", q, p);
// fourth type receives decimal and returns string
Func<decimal, string> four = (p) => string.Format("{0}", p);
Console.WriteLine(one.Invoke(25));
Console.WriteLine(two.Invoke(false, 76756566));
Console.WriteLine(three.Invoke(true, 50));
Console.WriteLine(four.Invoke(1.2m));
}
}
The output of the above code is −
25 False and 76756566 True and 50 1.2
Using Func with Methods
Func can also reference regular methods instead of just lambda expressions −
Example
using System;
class Program {
static int Square(int x) {
return x * x;
}
static string CombineText(string a, string b) {
return a + " " + b;
}
static void Main(string[] args) {
// Func referencing a method
Func<int, int> squareFunc = Square;
Func<string, string, string> combineFunc = CombineText;
// Using the Func delegates
Console.WriteLine("Square of 5: " + squareFunc(5));
Console.WriteLine("Combined: " + combineFunc("Hello", "World"));
// Can also call directly without Invoke()
Console.WriteLine("Square of 3: " + squareFunc.Invoke(3));
}
}
The output of the above code is −
Square of 5: 25 Combined: Hello World Square of 3: 9
Common Use Cases
-
Callback methods − Pass functions as parameters to other methods.
-
LINQ operations − Used extensively in LINQ for filtering, mapping, and transforming data.
-
Event handling − Store and invoke event handler methods dynamically.
-
Functional programming − Enable functional programming patterns in C#.
Example with LINQ
using System;
using System.Linq;
class Program {
static void Main(string[] args) {
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Using Func with LINQ Where method
Func<int, bool> isEven = x => x % 2 == 0;
var evenNumbers = numbers.Where(isEven);
// Using Func with LINQ Select method
Func<int, string> toString = x => "Number: " + x;
var stringNumbers = evenNumbers.Select(toString);
foreach (string item in stringNumbers) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Number: 2 Number: 4 Number: 6 Number: 8 Number: 10
Conclusion
The Func generic type in C# is a powerful delegate that represents methods returning values. It supports up to 16 input parameters and is widely used in LINQ, callbacks, and functional programming scenarios, making code more flexible and reusable.
