Click here to Skip to main content
15,886,258 members
Articles / Programming Languages / C#

Delegates - a 15 minutes quick start tutorial

Rate me:
Please Sign up or sign in to vote.
3.13/5 (26 votes)
9 Aug 20031 min read 88.5K   40  
Teaches you about delegates using an example program
using System;

namespace CSharpDelegate
{
	class SimpleClass
	{
		public class WorkerClass
		{
			//(1) First method (instance method) referenced by delegate:
			public int InstanceMethod(int nID, string sName)
			{
				int retval=0;
				retval=nID*sName.Length;
				Console.WriteLine("InstanceMethod fired!");
				return retval;
			}

			//(2) Second method (static method) referenced by delegate:
			static public int StaticMethod(int nID, string sName)
			{
				int retval=0;
				retval=nID*sName.Length;
				Console.WriteLine("StaticMethod fired!");
				return retval;
			}
		};

		//(1) That's your delegate - pay attention to delegate's signature. 
		//(2) It has the same signature as the methods to which it references (InstanceMethod and StaticMethod).
		public delegate int SomeDelegate(int nID, string sName);


		[STAThread]
		static void Main(string[] args)
		{

			//PART 1: invoking instance method			
			WorkerClass wr = new WorkerClass();
			SomeDelegate d1 = new SomeDelegate(wr.InstanceMethod); //Associating delegate with wr.InstanceMethod
			Console.WriteLine("Invoking delegate InstanceMethod, return={0}", d1(5, "aaa") ); //Invoking wr.InstanceMethod with input parameters.

			//PART 2: invoking static method
			SomeDelegate d2 = new SomeDelegate(WorkerClass.StaticMethod); //Associating delegate with WorkerClass.StaticMethod (NOTE: "wr" instance is NOT used. The class itself is used!!)
			Console.WriteLine("Invoking delegate StaticMethod, return={0}", d2(5, "aaa") ); //Invoking InstanceMethod with input parameters.

			//PART 3: MultiCAST!
			Console.WriteLine();
			Console.WriteLine("Testing delegate multi-cast..");
			SomeDelegate d3 = (SomeDelegate) Delegate.Combine(d1, d2);
			Console.WriteLine("Invoking delegate(s) d1 AND d2 (multi-cast), return={0} ", d3(5, "aaa") ); //Fire BOTH delegates (d1 AND d2) by firing d3!

			int num_method=d3.GetInvocationList().Length;
			Console.WriteLine("Number of methods referenced by delegate d3: {0}", num_method);

		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Hong Kong Hong Kong
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions