Core C#: Delegates

Microsoft has made it easier for aspiring developers to easily get started creating something when they came out with the C# language. However, there are definitely some advanced aspects of the language that take quite a bit longer than others to fully grasp. Delegates is one of those types of language features. Hopefully by the end of this article you'll have a deeper understanding of how utilize delegates in your C# code. 

I, myself, had trouble trying to fully understand what the purpose of delegates were. If you were to ask me before what a delegate was, I would just give a small answer indicating that it's similar to a method template. While that's true, it does quite a bit more.

The simplest way I know to go over delegates is to just go through a small example to allow you to also do the same so you can use that to experiment around with the code.

Let's go through this a bit. The first line is where you actually declare the delegate and give it a return type, name, and any parameters if it needs any. In our Main method is where we'll initiate a couple of variables to hold the delegates. The first one, myDelegate , does so using the method syntax. Our second variable, however, we declare it using an anonymous delegate, or a delegate without a function name.

After we've declared the our variables with our delegate, we can then invoke our delegate by calling it just like a method just like in lines 15 and 16 above. We declared our delegate to take in a single string parameter, so that's how we'll invoke it. 

Built in Delegate Types 

The folks at Microsoft were kind enough to give us a few built in delegates that we can also use within our code. Let's take a quick look at them. 

The first one, which may be the most commonly used, especially if you've messed quite a bit with the LINQ method syntax, is the Func  delegate type. This one will take in zero or more parameters and will return something back to the caller. In the example above I have one parameter of a string and I return back an integer.

The second one is the Action delegate type. Like the Func type it takes in zero or more parameters, however it returns void (returns nothing).

The third one, Predicate, is a bit closer to the Func type where it takes in parameters and returns. The difference with this is that it will always return a boolean true or false back to the caller.

Invocation Lists

One of the magic things that delegates have is they keep track of all invocations of it. When we instantiate our delegates we are actually adding to the invocation list. Another way to do this is to simply add a delegate to another delegate. 

We'll see the true power of delegates and their invocation lists in the next part, when we talk about events.

Small Change: Disqus Comments

Consuming the Jamendo API with AngularJS: HTTP Get