Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please Explain .NET Delegates

So I read MSDN and Stack Overflow. I understand what the Action Delegate does in general but it is not clicking no matter how many examples I do. In general, the same goes for the idea of delegates. So here is my question. When you have a function like this:

public GetCustomers(Action<IEnumerable<Customer>,Exception> callBack)
{
}

What is this, and what should I pass to it?

like image 255
Mike Diaz Avatar asked Mar 19 '10 01:03

Mike Diaz


People also ask

What is delegates in .NET with example?

Delegates allow methods to be passed as parameters. Delegates can be used to define callback methods. Delegates can be chained together; for example, multiple methods can be called on a single event. Methods don't have to match the delegate type exactly.

What is a delegate .NET framework?

Delegates in C# In the . NET environment, a delegate is a type that defines a method signature and it can pass a function as a parameter. In simple words we can say delegate is a . NET object which points to a method that matches its specific signature.

What is delegate explain?

1 : to entrust to another delegate authority delegated the task to her assistant. 2 : to appoint as one's representative. intransitive verb. : to assign responsibility or authority a good manager knows how to delegate.

What are delegates in C# used for?

Understanding delegates in C# Delegates are used to define callback methods and implement event handling, and they are declared using the “delegate” keyword. You can declare a delegate that can appear on its own or even nested inside a class.


1 Answers

it expects a function that takes IEnumerable and Exception and returns void.

void SendExceptionToCustomers(IEnumerable<Customer> customers, Exception ex) {
   foreach(var customer in customers)
      customer.SendMessage(ex.Message);
}

GetCustomers(SendExceptionToCustomers);

btw, GetCustomers seems like a terrible name for this function -- it's asking for an action, so its more like DoSomethingToCustomers

EDIT in response to comment


Ok Makes sense, So now why even bother with having a GetCustomer Function? Can't I do that same thing with your function if i Just rename it GetCustomer?

Well, what's happening here is the caller can specify some action. Suppose GetCustomers is implemented like this:

public void GetCustomers(Action<Enumerable<Customer>, Exception> handleError) {
    Customer[] customerlist =  GetCustomersFromDatabase();
    try {
        foreach(var c in customerList) 
            c.ProcessSomething()
    } catch (Exception e) {
        handleError(customerList, e);
    }
}

then you could call Getcustomers from somewhere on a commandline program, and pass it

GetCustomers((list, exception) => { 
    Console.WriteLine("Encountered error processing the following customers");
    foreach(var customer in list) Console.WriteLine(customer.Name);
    Console.WriteLine(exception.Message);
}); 

while you could call GetCustomers from a remote application, for example, and pass it

Getcustomers((list, exception) => { 
    // code that emails me the exception message and customer list
})


Also, Slak's comment suggests another reason for delegate parameter -- GetCustomers does retrieve the customers, but asynchronously. Whenever it is done retrieving the customers, it calls the function you give it with either the customerlist or an exception, if an exception occurred.
like image 149
Jimmy Avatar answered Oct 05 '22 12:10

Jimmy