Simple C# Delegate Example

May 5, 2008 at 4:23 pm Leave a comment

Simple example of passing a delegate to a method to extend the functionality of the recieving method. This allows us to reuse the code in the recieving method since it can be combined with the method passed with the delegate to produce new functionality.

using System;
using System.Collections.Generic;
using System.Text;

// Delegates are used here to provide a method containing common functionality
// a way of using a method passed in (via a delegate) to extend functionality.
// This is a way of re using code since common functionality is extended via
// the use of delegates that pass in specialist functionality


    public class UserClass
    {
        // Method that takes a delegate as a parameter and uses it to complete its work
        public void ReturnText(ProcessDelegate newDelegate)
        {
            string stubString = "Stub ";

            string getDelegateResponse = newDelegate();

            stubString += getDelegateResponse;

            Console.WriteLine( stubString );
        }
    }

    public delegate string ProcessDelegate();

    public class MainClass
    {
        // Instance method to be passed to delegate
        string AppendText()
        {
            Random random = new Random();

            string txt = "";

            txt += random.Next(0, 100000).ToString();

            return txt;
        }

        // Static method to be passed to delegate
        static string StaticAppendText()
        {
            Random random = new Random();

            string txt = "";

            txt += random.Next(0, 100000).ToString();

            txt += (" (Static)");

            return txt;
        }

        static void Main(string[] args)
        {
            // Create an instance of the class which contains a method that uses a delegate
            UserClass newUserClass = new UserClass();

            // Call the user class's method passing a methed of an instance object
            MainClass newMainClass = new MainClass();
            newUserClass.ReturnText(new ProcessDelegate(newMainClass.AppendText));

            // Call the user class's method passing a static method
            newUserClass.ReturnText(new ProcessDelegate(StaticAppendText));

            Console.ReadLine();
        }
    }
Advertisements

Entry filed under: C#.

Simple JavaScript Image Preloader ASP.NET / C# Image Optimization

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

May 2008
M T W T F S S
« Mar    
 1234
567891011
12131415161718
19202122232425
262728293031  

Most Recent Posts


%d bloggers like this: