Func Delegate Using Lambda Expression In C#

Introduction

In this article, I'll try to explain a cool feature introduced with .NET 3.5. Known as Func, also named by some developers 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.

encapsulates method

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

Definition of Func<> in C#

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

func <>

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.

func<>delegates

Definition of Func using F12 in C#

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.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
    /// <typeparam name="TResult">The type of the return value of the method that this delegate encapsulates.</typeparam>
    /// <param name="arg1">The first parameter of the method that this delegate encapsulates.</param>
    /// <param name="arg2">The second parameter of the method that this delegate encapsulates.</param>
    /// <returns>The return value of the method that this delegate encapsulates.</returns>
    public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
}

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

At the initial level of the 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 the world of .NET</b>, There are a lot of new and emerging things in .NET<br>"
                  + "<h1>Make it your passion to help the community and cherish 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>".

What is the 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.

replace extra func<>

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<>.

press f5 func<>

A sample application is attached as a reference.

Hope you enjoyed this demonstration.


Similar Articles