The Artima Developer Community
Sponsored Link

Java Buzz Forum
Having fun with C# delegates

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Simon Brown

Posts: 636
Nickname: simonbrown
Registered: Jun, 2003

Simon Brown is a Java developer, architect and author.
Having fun with C# delegates Posted: Oct 28, 2008 3:35 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Simon Brown.
Original Post: Having fun with C# delegates
Feed Title: Simon Brown's weblog
Feed URL: http://www.simongbrown.com/blog/feed.xml?flavor=rss20&category=java
Feed Description: My thoughts on Java, software development and technology.
Latest Java Buzz Posts
Latest Java Buzz Posts by Simon Brown
Latest Posts From Simon Brown's weblog

Advertisement

Although I couldn't quite get my first example of C# delegates working, I've been having a lot of fun using them elsewhere. In this particular example, I've been using delegates to point to validation methods as a really simple way to conditionally invoke different blocks of validation code. However, the really nice part about all of this is that C# supports the chaining together of delegates, without the need to manually manage a list of objects yourself. Basically, once you have a delegate defined, you simply use the overloaded += operator. Here's an example.

// declare the delegate
delegate void ValidateDelegate();
ValidateDelegate validators;

// add some delegates
validators += validateSomething;
validators += validateSomethingElse;

// call all delegates in the chain
validators(); 

In Java, I would have implemented something similar by defining a simple interface and then perhaps implementing that interface using a bunch of anonymous inner classes. The nice thing about delegates is that you can do all of this in a much lighter and more dynamic way.

Read: Having fun with C# delegates

Topic: Links for 2008-10-22 [del.icio.us] Previous Topic   Next Topic Topic: When Agile and Cloud Computing Meet from PeopleOverProcess.com

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use