Saturday, October 13, 2007

Delegates in C#

So you instantiate a person and have a baby class, lets call it aBaby. Now your instance aBaby will occasionally (or regularly) need its diaper changed, so on intervals you check to see if it's dirty but wouldn't it be better for the baby to actually tell you that its nappy needs to be changed?

In an introduction to object oriented programing you would have learned how to create an instance of a class, call its methods and have data returned from the methods. But having the instance send data back to the class that created the instance on intervals or events is a little more tricky. If you see this scenario or design pattern then you need something like Delegates in C#, sure you could probably use a static class and throw data in to a variable that every other class can access - but wheres the class in that, not to mention the disastrous love you experience when you start running multiple threads.

Any way check out the video in the link - brilliant and simple introduction in to the delegates concept and one I'll be putting in place shortly.