Using delegation methods and methods

I see the growing use of delegate types offered in the System namespace ( Action<T>; Predicate<T>, etc.). Since these are delegates, I understand that they should be used where we have traditionally used delegates in the past (asynchronous calls, triggering threads, handling events, etc.). It is simply preference or practice is believed to use these types of delegates in scenarios such as below; instead of using method calls that we 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 than using declared or anonymous methods. I am starting to code this path and wonder what is the point of view in this regard. The above example relates to a method. Is this delegate excessive?

+3
1

foreach - . . , .

- LINQ - .

- , ... , . ( ), . , . , , .

+4

Source: https://habr.com/ru/post/1749736/


All Articles