Create elegant code with Action delegate and List.ForEach method(dotnettipoftheday.org)

submitted by manovichmanovich(755) 4 years, 6 months ago

Small example of how to use Action delegate to perform some action on all elements of a collection.

9 comments |category: |Views: 645

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by bladefistbladefist(471) 4 years, 6 months ago 0

wow crazy tip. good post!

Reply

posted by JudahGabrielJudahGabriel(814) 4 years, 6 months ago 0

Cool stuff! I like it!

Reply

posted by yesthatmcgurkyesthatmcgurk(4063) 4 years, 6 months ago 0

You don't even need a delegate to do this sort of thing. Just use a method that has the same signature. For example, I use Regex' IsMatch method all the time with the Filter<T> method of Array.

Regex foo = new Regex(".*Lol.*");

string[] linesThatContainLol = Array.Filter<string>(sourceArray, foo.IsMatch);

Reply

posted by yesthatmcgurkyesthatmcgurk(4063) 4 years, 6 months ago 0

1) The tip doesn't specifically cast to the Action delegate, which is what I was implying above
2) I hate emoticons. They must die. Die emoticons die.

Reply

posted by fquednaufquednau(404) 4 years, 6 months ago 0

@yesthatmcgurk: Nice one. I am kinda amazed this level of tips gets that many kicks but fair enough, the comments make up for it. Here's another one...

string[] a = { "1", "2" };
int[] b = Array.ConvertAll<string,int>(a, int.Parse);

Reply

posted by fquednaufquednau(404) 4 years, 6 months ago 0

To have automatic emoticon functionality where code may be potentially posted is indeed very wrong.

Reply

posted by simonebsimoneb(5450) 4 years, 6 months ago 0

We're working on this.

Reply

posted by bladefistbladefist(471) 4 years, 6 months ago 0

the following code cures cancer
:D=):););(:(:,(;):D

Reply

posted by jessejesse(100) 4 years, 6 months ago 0

@bladefist: LOL

Reply

information Login or create an account to comment on this story