Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / C#

Using Delegates As Parameters

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
13 Jan 2010CC (ASA 2.5)3 min read 9K   7   1
This post goes over a sample scenario where you might use delegates as arguments instead of writing multiple functions to perform different tasks.

I'm not sure about the community stance on passing around delegates as arguments but personally I love it. I think there is something magical about the way you can provide instructions on how how to do something and then pass it into another method and let it do its job without needing to think about it. It is almost like writing a code template and then filling in the blanks with the specifics of what you need.

Using delegates as arguments can simplify code and in some cases reduce it. This post goes over a sample scenario where you might use delegates as arguments instead of writing multiple functions to perform different tasks.

Simple Resource Loader

Let’s pretend we have a class that is responsible for loading and saving files from an external resource. We could write a class like the one below:

C#
//handles loading resources for the application
public static class ResourceLoader {

    //returns the bytes for a resource
    public static byte[] LoadBytes(string resource) {
        string path = ResourceLoader._GetResourcePath(resource);
        return File.ReadAllBytes(path);
    }

    //returns an XML document resource
    public static XDocument LoadXmlDocument(string resource) {
        string path = ResourceLoader._GetResourcePath(resource);
        return XDocument.Load(path);
    }

    //returns a bitmap resource
    public static Bitmap LoadBitmap(string resource) {
        string path = ResourceLoader._GetResourcePath(resource);
        return Bitmap.FromFile(path) as Bitmap;
    }

    //returns the string text for a resource
    public static string LoadText(string resource) {
        string path = ResourceLoader._GetResourcePath(resource);
        return File.ReadAllText(path);
    }

    //generates a path to a resource
    private static string _GetResourcePath(string file) {
        return Path.Combine(@"c:\path\to\files\", file);
    }
}

Nothing is really wrong with this code. If we need to add a new type to this class, then we simply create a new method and plug it in. Additionally, each of the methods could use some sort of exception handling in case the conversion doesn't go so well. As you can imagine, the more try catch blocks we add, the larger this class starts to get.

However, if you think about it, all of these resources could be handled roughly the same way. They all need a way to convert bytes to whatever type you're wanting.

Using A Delegate To Fill In The Blank

So instead, let's address this problem using a delegate to handle the conversion of bytes to the type that we need.

C#
//handles loading resources for the application
public static class ResourceLoader {

    //returns the bytes for a resource
    public static T Load<T>(string resource, Func<byte[], T> convert) {

        //find the correct path
        string path = Path.Combine(@"c:\path\to\files\", resource);
        byte[] bytes = File.ReadAllBytes(path);

        //attempt convert the file
        try {
            return convert(bytes);
        }
        //if it fails, forward the error to the caller
        catch (Exception ex) {
            throw new FormatException(
                string.Format("Could not load resource '{0}' as a type {1}", 
					resource, typeof(T).Name),
                ex
                );
        }
    }
}

Great — Now we can provide any method we want to format the bytes and then return the type we're looking for. So for example, instead of calling ResourceHandler.LoadBitmap we can use our new method as shown below:

C#
Bitmap bitmap = ResourceLoader.Load("test.jpg", (bytes) => {
    using (MemoryStream stream = new MemoryStream(bytes)) {
        return Bitmap.FromStream(stream) as Bitmap;
    }
});

Pretty slick, right? …oh, wait… this code is longer and more difficult to use… This can't be right!

Bringing The Concept Together

Clearly, the example above isn't improving anything for us. The code is longer and requires that we write the same functionality for reading a resource in multiple places. Even though this code is more flexible, it also requires more work. So instead, let's write some definitions of common conversions as part of our class.

C#
//handles loading resources for the application
public static class ResourceLoader {

    //Load<T>(resource, convert)
    //snip...

    //converts bytes to a bitmap
    public static readonly Func<byte[], Bitmap> AsBitmap = (bytes) => {
        using (MemoryStream stream = new MemoryStream(bytes)) {
            return Bitmap.FromStream(stream) as Bitmap;
        }
    };

    //converts bytes to an XML document
    public static readonly Func<byte[], XDocument> AsXml = (bytes) => {
        using (MemoryStream stream = new MemoryStream(bytes)) {
            string xml = Encoding.UTF8.GetString(bytes);
            return XDocument.Parse(xml);
        }
    };

    //converts bytes to a string
    public static readonly Func<byte[], string> AsText = (bytes) => {
        return Encoding.UTF8.GetString(bytes);
    };

    //simply returns the byte array
    public static readonly Func<byte[], byte[]> AsBytes = (bytes) => bytes;
}

Now, instead of needing to manually create a delegate to handle the conversion process, we can write code like the example below:

C#
//Loads a resource by passing the static delegate that is part of the class we created
Bitmap bitmap = ResourceLoader.Load("test.jpg", ResourceLoader.AsBitmap);

By using delegates as an argument, we allow our loading function to be flexible enough to accept other methods for performing the conversion (that we can create as we need them) or the common conversion methods that we added as part of the class.

You'll also notice that we don't need to declare the generic type we're wanting to have returned since it can be inferred from the return type of the delegate we pass in! (so cool… to me at least)

Of course, there are about a hundred different ways that can already load resources in .NET but you could apply this same concept in many other areas of applications you develop.

How could you use delegates to create “templates” for code?

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralExcellent! Pin
Your Display Name Here21-Oct-10 14:35
Your Display Name Here21-Oct-10 14:35 
Agile has been taking me to delegate and event level programming. In my opinion you've demonstrated how to factor out commonality in a very nice way. Passing in a delegate is something refactor and really taking the "Code once Mantra" to the next level. I rate this a 5.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.