Func Delegate Using Lambda Expression in C#

In this article I'll try to explain a cool feature introduced with .NET 3.5. Known as Func, also named by some developer as a readymade delegate.

Func encapsulates a method with two parameters and returns a value of the type specified by the TResult parameter. It has a few overloaded methods as depicted below:

Func1.jpg

If you look into the image shown above then it shows you five overloaded methods.

Definition of Func<>

I've used a delegate that contains the following syntax defined as below:

Func2.jpg

Now let's discuss how it works and accepts parameters. In the Func<> delegate there are three params being passed, the first one is of string type named "a", the second "b" is also a string type and the third is a result type that is also a string type.
Func3.jpg

If you find the definition of Func using F12 then it gives you the following details:

namespace System

{

    // Summary:

    //     Encapsulates a method that has two parameters and returns a value of the

    //     type specified by the TResult parameter.

    //

    // Parameters:

    //   arg1:

    //     The first parameter of the method that this delegate encapsulates.

    //

    //   arg2:

    //     The second parameter of the method that this delegate encapsulates.

    //

    // Type parameters:

    //   T1:

    //     The type of the first parameter of the method that this delegate encapsulates.

    //

    //   T2:

    //     The type of the second parameter of the method that this delegate encapsulates.

    //

    //   TResult:

    //     The type of the return value of the method that this delegate encapsulates.

    //

    // Returns:

    //     The return value of the method that this delegate encapsulates.

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

}

                                       Func4.jpg

Internally it's a delegate that accepts two params and returns a TResult.

At the initial level of code segment I've set a description that contains some delimiters. My task is to remove all of it from the description.

string description = "<b>Hi Welcome to world of .net</b> ,There are lot of new and emerging things into .net</br>"
                                 +
"<h1>Make it your passion to help community and cheer for every moment</h1></br>"


Declaration of Func<>

Func<string, string, string> replaceExtra = (a, b) => a.Replace(b, string.Empty);

The purpose of this delegate is to replace all the occurrences of delimiters like "<b>,</b>,</br><h1></h1>".

Use of replaceExtra Func<>

The code segment shown below uses the replaceExtra that takes two params (both are of string type) and returns the value as a string type also.

description = replaceExtra(description, charsToReplace[0]);

Now let's run this and examine the working behavior:

When we run the program initially without using the replaceExtra Func<> delegate, it prompts the following screen:

Func5.jpg

Now I use the following lines of code and try to replace all occurrences of delimiters:

description = replaceExtra(description, charsToReplace[0]);

description = replaceExtra(description, charsToReplace[1]);

description = replaceExtra(description, charsToReplace[2]);

description = replaceExtra(description, charsToReplace[3]);


Again press F5 and see the magic of Func<>.

Func6.jpg

A sample application is attached as a reference.

Hope you enjoyed this demonstration.

Keep coding and Smile.

Up Next
    Ebook Download
    View all
    Learn
    View all