<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn's C# Programming Blog &#187; delegate</title>
	<atom:link href="http://www.dijksterhuis.org/tag/delegate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Delegates in C#</title>
		<link>http://www.dijksterhuis.org/using-delegates-in-c/</link>
		<comments>http://www.dijksterhuis.org/using-delegates-in-c/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 08:38:03 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[delegation]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=167</guid>
		<description><![CDATA[What is a delegate and why would you want to use them?
Calling methods with parameters comes natural in programming, but what if you would like to pass the methods themselves to another method?
To give an example: You might want to set a timer and have the timer call a particular method when the time comes. [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><strong>What is a delegate and why would you want to use them?</strong></p>
<p>Calling methods with parameters comes natural in programming, but what if you would like to pass the methods themselves to another method?</p>
<p>To give an example: You might want to set a timer and have the timer call a particular method when the time comes. To avoid having to hard code each possible action together with its own timer you might want to pass the action method in your call to the timer itself.  In C# this is called delegation.</p>
<p><span id="more-167"></span></p>
<p>Another use for delegation is in sort routines. The actual sorting code is common, but the action that decides which element comes before another depends on what you are trying to sort. In this situation you could pass a method to the sorting routine that provides this knowledge.</p>
<p>A pseudo code example for sorting an array of integers:</p>
<pre class="brush: c#">
Int[] A;

int LargerThan1( int C, int D )
{
   return (C &gt; D);
}

int LargerThan2( int C, int D )
{
   if (Today == TuesDay) return (D &gt; C);
   return (C &gt; D);
}

QuickSort( A[] , LargerThan1 );
QuickSort( A[] , LargerThan2 );  // Same, but with a twist on Tuesdays
</pre>
<p>Delegation comes in handy when you need to call different methods from inside other methods. This is quite common in event driven programmes (such as the above timer example).</p>
<p>There are some drawbacks to delegation however:</p>
<ul>
<li>The implementation of a delegate must be static.
<li>Because of this delegates cannot call other methods or properties of the object that implements them.
</ul>
<p>The following code shows the minimal implementation of a delegate. The code defines a delegate, and two implementations. It then calls a method, passing each implementation of the delegate method in turn. </p>
<pre class="brush: c#">
using System;

namespace DelegateDemo1
{
    class MainClass
    {

        // Delegate_Example
        //
        // To create a delegate we need to define a method. Important are the
        // return type, name of the delegate and the parameters.

        public delegate void Delegate_Example(string message);

        // DelegateModel_A
        //
        // This method exactly follows the delegate model (return type: void and one string parameter)
        //
        // An implementation of the delegate can only be static. 

        public static void DelegateModel_A(string message)
        {
            Console.WriteLine(&quot;Model_A: {0}&quot;,message);
        }

        public static void DelegateModel_B(string message)
        {
            Console.WriteLine(&quot;Model_B: {0}&quot;,message);
        }

        // Example_method
        //
        // This method accepts our Delegate as a parameter, and calls it in turn.
        // Normally this routine would perform a sort action, or something similar
        // that would need the delegate to perform some action.

        public static void Example_method(Delegate_Example E)
        {
            // Here we do some magic

            // We call the method passed to us
            E(&quot;Hello World&quot;);

            // Here we do some more magic

        }

        static void Main()
        {
            // We can assign the implementation methods like below
            Delegate_Example A = DelegateModel_A;
            Delegate_Example B = DelegateModel_B;

            // Then we pass them to a method that knows how to call this delegate
            Example_method(A);
            Example_method(B);
        }

    }

}
</pre>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/using-delegates-in-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
