Skip to content

Tag: delegates

Anonymous Methods C#

Last time we talked about Delegates, now we’ll look into the concept of anonymous methods that truly shines with the use of delegates. The Delegate ‘Clutter’ In order to use a Delegate you must do 4 things: Anonymous Methods at work By using anonymous methods, we could change step 3 and assign a method ‘on the fly’ without creating a new one The code can be simplified even more Keep in mind that if you have break/continue statements inside your anonymous method the target should remain inside the code block! If you want more information about the scope of the…

Leave a Comment

Delegates C#

A Delegate by definition is a person sent or authorized to represent others. The key concept here is ‘Represent others’. Delegates are used to represent other methods. Delegates at work Start by looking the example below, this is how we declare a simple Delegate. It is just like any other variable but instead of holding an integer value, it holds a method that can be executed. Now that we have declared our delegate we associate any method from any class as long as it matches the signature and the return type of the delegate. The method below is a valid…

Leave a Comment