Click here to Skip to main content
11,429,382 members (63,918 online)
Click here to Skip to main content

Tagged as

Uses of Func in C#

, 11 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Uses of Func in C#

What is Func?

Func in short is parameterized delegate. In C#, a delegate instance points towards a method. When a caller invokes the delegate, it calls its target method. This way, the caller is not invoking the target method rather invoking the delegate which can call the target method. We do it because it creates an abstraction on invoking the target method. We of course always can invoke a method directly but decoupling of the client and target method is sometimes a need or gives us more flexibility to make things clean and simple.

We can use Func delegate to represent a method that can be passed as a parameter without explicitly declaring a custom delegate.

Why I said it's a parameterized delegate:

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

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

... and so on, up to T16   

The Problem

To understand use of Func, we must understand what problems it can solve. Let's say we have an int array here.

IEnumerable<int> numbers = new[] {3, 4, 7, 1, 8, 10, 21, 5, 9, 11, 14, 19};

It's a simple array. If we want to print the numbers which are greater than 10, we will write a method like below. We will send the array as parameter and will get another array which will contain the numbers that are greater than 10.

public IEnumerable<int> GetGreaterThanTen(IEnumerable<int> numbers)
{
    foreach (int number in numbers)
        if (number > 10)
            yield return number;
} 
…and then if we want to print the numbers which will have result less than 1 after divided by 5, we will write a method:
public IEnumerable<int> GetGreterThanOneAfterDevidedByFive(IEnumerable<int> numbers)
{
    foreach (int number in numbers)
        if ((number/5) > 1)
            yield return number;
}

The Solution

In the above way, we will add more methods as the new conditions come. But there is a better way to face this problem. We can and should use delegate to avoid rewriting a fresh method every time we get by a new rule we want to apply for this array. Use of Func as method parameter we will have the leverage to decouple the caller and the function we want to use. Func will pass the rule to the method body and will apply it.

public IEnumerable<int> GetNumbers(IEnumerable<int> numbers, Func<int, bool> numberResolver)
{
    foreach (int number in numbers)
        if (numberResolver(number))
            yield return number;
}
IEnumerable<int> numbers = new[] {3, 4, 7, 1, 8, 10, 21, 5, 9, 11, 14, 19};

IEnumerable<int> greaterThanTen = GetNumbers(numbers, x=>x>10);
greaterThanTen.ToList().ForEach(Console.WriteLine);

Console.WriteLine("\n");

IEnumerable<int> greaterThanOneAfterDividedByFive = GetNumbers(numbers, n => (n/5) > 1);
greaterThanOneAfterDividedByFive.ToList().ForEach(Console.WriteLine);

The above code is pretty self-explanatory. Func uses Lambda expression which is essentially an anonymous function.

For extensibility standpoint, Func is very effective. While writing various extension methods, you will find it extremely useful.

Getting TResult

The x-factor of Func is probably Func<TResult>. For functional programming, the return type has great use. For example:

Func<int, int> square = num => num*num;
Console.WriteLine(square(3));

…also, we can bring the previous example here see a bit more complex scenario.

IEnumerable<int> numbers = new[] { 3, 4, 7, 1, 8, 10, 21, 5, 9, 11, 14, 19 };
Func<IEnumerable<int>, IEnumerable<int>> getGreaterThanTen = nums => 
{
    var array = new List<int>();
    nums.ToList().ForEach(x =>
    {
        if (x > 10)
            array.Add(x);
    });
    return array;
};

var result = getGreaterThanTen(numbers);

result.ToList().ForEach(Console.WriteLine);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Md Nazmoon Noor
Software Developer (Senior)
Bangladesh Bangladesh
No Biography provided

Comments and Discussions

 
QuestionVery nice. Easy to understand! Pin
Member 1086450826-Mar-15 5:14
memberMember 1086450826-Mar-15 5:14 
QuestionMy Vote of 5 Pin
Faisal(mfrony)14-May-14 2:33
memberFaisal(mfrony)14-May-14 2:33 
GeneralMy vote of 5 Pin
Monjurul Habib14-May-14 2:09
professionalMonjurul Habib14-May-14 2:09 
GeneralRe: My vote of 5 Pin
Md Nazmoon Noor14-May-14 2:24
memberMd Nazmoon Noor14-May-14 2:24 
QuestionWon't Work Pin
hansonprogrammer12-May-14 18:23
memberhansonprogrammer12-May-14 18:23 
AnswerRe: Won't Work Pin
Md Nazmoon Noor13-May-14 2:42
memberMd Nazmoon Noor13-May-14 2:42 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun11-May-14 22:28
memberHumayun Kabir Mamun11-May-14 22:28 
GeneralRe: My vote of 5 Pin
Md Nazmoon Noor11-May-14 22:35
memberMd Nazmoon Noor11-May-14 22:35 
GeneralMy vote of 2 Pin
Alexander Sharykin11-May-14 21:12
memberAlexander Sharykin11-May-14 21:12 
QuestionFormat Pin
Nelek11-May-14 7:15
memberNelek11-May-14 7:15 
AnswerRe: Format Pin
Md Nazmoon Noor11-May-14 8:11
memberMd Nazmoon Noor11-May-14 8:11 
GeneralRe: Format Pin
Nelek11-May-14 8:42
memberNelek11-May-14 8:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 11 May 2014
Article Copyright 2014 by Md Nazmoon Noor
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid