Using delegate Types vs methods

Posted by Grant Sutcliffe on Stack Overflow See other posts from Stack Overflow or by Grant Sutcliffe
Published on 2010-06-12T14:01:39Z Indexed on 2010/06/12 14:02 UTC
Read the original article Hit count: 200

Filed under:
|

I see increasing use of the delegate types offered in the System namespace (Action; Predicate etc). As these are delegates, my understanding is that they should be used where we have traditionally used delegates in the past (asynchronous calls; starting threads, event handling etc). Is it just preference or is it considered practice to use these delegate types in scenarios such as the below; rather than using calls to methods we have declared (or anonymous methods):

public void MyMethod {

  Action<string> action = delegate(string userName
  {
        try
        {
           XmlDocument profile = DataHelper.GetProfile(userName);
           UpdateMember(profile);
        }
        catch (Exception exception)
        {
           if (_log.IsErrorEnabled) _log.ErrorFormat(exception.Message);
           throw (exception);
        }
  };

GetUsers().ForEach(action); }

At first, I found the code less intuitive to follow than using declared or anonymous methods. I am starting to code this way, and wonder what the view are in this regard. The example above is all within a method. Is this delegate overuse.

© Stack Overflow or respective owner

Related posts about c#

Related posts about c#2.0