Traditional loop versus Action delegate in C#

Posted by emddudley on Stack Overflow See other posts from Stack Overflow or by emddudley
Published on 2010-03-29T14:16:52Z Indexed on 2010/03/29 14:23 UTC
Read the original article Hit count: 413

Filed under:
|
|

After learning about the Action delegate in C# I've been looking for ways I can best use it in my code. I came up with this pattern:

Action<string> DoSomething = (lSomething) =>
{
    // Do something
};

DoSomething("somebody");
DoSomething("someone");
DoSomething("somewhere");

If I were to have used a traditional loop, it would look something like this:

List<string> lSomeList = new List<string>();
lSomeList.Add("somebody");
lSomeList.Add("someone");
lSomeList.Add("somewhere");

foreach (string lSomething in lSomeList)
{
    // Do something
}

Are there any appreciable differences between the two? To me they look equally easy to understand and maintain, but are there some other criteria I might use to distinguish when one might be preferred over the other?

© Stack Overflow or respective owner

Related posts about action

Related posts about delegate