C# how to pass a function with parameters to another function


We will be talking now about how to pass a function with parameters to another function especially when you have different functions returning the same data that should be manipulated or processed to be able to apply abstraction and flexibility to your application by applying the rule of separation of concerns.

so we start

first you need to write the functions that you need which return a similar output such as you are reading data and you want top process it and the problem that the data is coming from different sources such as CSV files, databases, or web services. So for me I will refer to the previous article that I wrote about C# Get Exchange Distribution and Dynamic Distribution Lists and their members

Let’s assume that I want to send emails to distribution list or a dynamic distribution list so we need to create a mailer function that accepts function with parameters and it will look like below. The Function should accept delegate function with parameters as an array of objects. Then we execute the (DynamicInvoke for that delegate function)

public void SenMail(Delegate method,params object[] args)
            List<string> emailAddresses = (List<string>)method.DynamicInvoke(args);
            //List<string> emailAddresses = new List<string>();
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Host = mailhost;

            //MailMessage mail = new MailMessage(notificationsEmail.Address, @emailAddress);
            if (emailAddresses.Count > 0) 

                foreach (string emailAddress in emailAddresses)

                    MailMessage mail = new MailMessage(replyTo.Address, emailAddress);

                    mail.IsBodyHtml = true;         
                    mail.Subject = "this is a spam test";
                    mail.Body = "this is a spam test ";


But to be able to do so our functions should be prepared to be passed as delegate function so we do the following

public delegate List<string> GetMembersEmail(string DNORFILTER);

//create appropriate functions for DL and DDL
GetMembersEmail DLEmails = new GetMembersEmail(dlu.listDLMembers);
GetMembersEmail DDLEmails = new GetMembersEmail(dlu.listDDLMembers);

//Prepair Functions to be passed to mailer
Func<string, List<string>> dluHandler = (param) => DLEmails(param);
Func<string, List<string>> ddluHandler = (param) => DDLEmails(param);

//Then call the senmail function by passing the delegate 
mailer.SenMail(ddluHandler, new object[] { "(LDAP Filter)" });
mailer.SenMail(dluHandler, new object[] { "DL DN" });

and thats about it I hope that you will find that article useful for you.

Posted in Programming and tagged , , , , , , , , , , , , , . Bookmark the permalink. RSS feed for this post. Leave a trackback.

2 Responses to C# how to pass a function with parameters to another function

  1. Tommy Long says:

    Couldn’t you just have utilised lambda closure and done this much more simply?

    public void SenMail(Func strategy)
    var emailAddresses = strategy();
    /// etc

    var dluHandler = () => GetMembersEmail(dlu.listDLMembers);

    Your dynamic invocation and object array are just unnecessary?

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

Swedish Greys - a WordPress theme from Nordic Themepark.