<feed xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US">
    <title>Huzefa Katwarawala</title>
    <link rel="self" type="application/xml" href="http://geekswithblogs.net/HuzefaKatwara/Atom.aspx" />
    <subtitle type="html"> </subtitle>
    <id>http://geekswithblogs.net/HuzefaKatwara/Default.aspx</id>
    <author>
        <name>HuzefaKatwara</name>
        <uri>http://geekswithblogs.net/HuzefaKatwara/Default.aspx</uri>
    </author>
    <generator uri="http://subtextproject.com" version="Subtext Version 0.0.0.0">Subtext</generator>
    <updated>2009-06-23T02:28:31Z</updated>
    <entry>
        <title>Real World Interfaces in C#</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/HuzefaKatwara/archive/2009/06/23/real-world-interfaces-in-c.aspx" />
        <id>http://geekswithblogs.net/HuzefaKatwara/archive/2009/06/23/real-world-interfaces-in-c.aspx</id>
        <published>2009-06-23T02:24:12-05:00:00</published>
        <updated>2009-06-23T02:24:12Z</updated>
        <content type="html">&lt;p&gt;I have been on a bit of a "quest" lately to learn interfaces better. While at work I hit up a couple of fellow developers on "why" you should use interfaces in the real world. Believe it or not after going through some code and getting some commentary on it I think I have an understanding of interfaces.  Here is my understanding in a possible "real world" situation. &lt;/p&gt;
&lt;p&gt;Let’s say you want to render out commonly used objects to the screen.  They can be textbox's, images, or even labels.  Each of these might have a common set of properties like title, id, width,  and height; plus a method like Render() to show it on the screen.  Now in .NET we might call these objects controls so we will use that naming convention for example sake.  You might create an interface called IControl that all controls will inherit from.  One reason to use an interface instead of a separate class is because the render method could be different for all the different controls, but is all that is needed to be called in order to show the control. &lt;/p&gt;
&lt;p&gt;So, let’s look at some basic code.  First we will look at the interface. &lt;/p&gt;
&lt;p&gt;&lt;img height="161" alt="IControl Interface" width="253" src="http://www.buddylindsey.com/Blog/image.axd?picture=IControlInterface.PNG" /&gt; &lt;/p&gt;
&lt;p&gt;This interface IControl has our common properties and the render method. We are going to implement different render methods for each of our different controls so we might not necessarily want to create a control class to inherit from. To illustrate this more let’s look at two different controls implementing the IControl interface &lt;/p&gt;
&lt;p&gt;TextBox &lt;/p&gt;
&lt;p&gt;&lt;img height="627" alt="TextBox Class Implementing IControl" width="543" src="http://www.buddylindsey.com/Blog/image.axd?picture=TextBoxClass.PNG" /&gt; &lt;/p&gt;
&lt;p&gt;Label &lt;/p&gt;
&lt;p&gt;&lt;img height="621" alt="Label Class Implementing IControl" width="548" src="http://www.buddylindsey.com/Blog/image.axd?picture=LabelClass.PNG" /&gt; &lt;/p&gt;
&lt;p&gt;If you notice both the TextBox and the Label controls have slightly different render methods, but both contain the same properties because of the IControl interface. This is important to note because we KNOW that if the IControl interface is being used that you can always call the methods and properties from them. So that means you can make a method like the following which will always call the render method as long as the object implements the IControl interface no matter what the control is. &lt;/p&gt;
&lt;p&gt;&lt;img height="76" alt="OutPut Method that calls IControl Render Method" width="316" src="http://www.buddylindsey.com/Blog/image.axd?picture=OutPutMethod.PNG" /&gt; &lt;/p&gt;
&lt;p&gt;With this you can create an instance of TextBox and Label and pass both of them to the above method and they will call the render method and output the correct information. &lt;/p&gt;
&lt;p&gt;Here is some “example” code on how-to use and put together everything above. &lt;/p&gt;
&lt;p&gt;&lt;img height="333" alt="Main Method Implementing Controls" width="359" src="http://www.buddylindsey.com/Blog/image.axd?picture=MainMethod.PNG" /&gt; &lt;/p&gt;
&lt;p&gt;&lt;font size="3"&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/font&gt; &lt;/p&gt;
&lt;p&gt;To wrap it up you can make an interface and an interface basically says “hey I have at the very least these properties and methods and you can use them however you need without worrying about what they do to actually use them” Which means that you can write any code you want to implement each of the properties and methods and as long as you use the interface as your “datatype” there is no need to worry about what the code actually does for each and every object that implements the interface. In the case of the example there is no need to worry about how many or what the controls actually do when rendered because they will all be rendered by using the output method since the datatype of the parameter is IControl. &lt;/p&gt;
&lt;p&gt;Please, feel free to leave comments and critiques on this. I am still learning about interfaces and this is as “real world” as I could figure out on how to use interfaces as that is what helps learn. Any help would be great. I am sorry for the length I just wanted to be thorough, I hope I was. &lt;/p&gt;&lt;img src="http://geekswithblogs.net/HuzefaKatwara/aggbug/132991.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/HuzefaKatwara/comments/132991.aspx</wfw:comment>
        <slash:comments>3</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/HuzefaKatwara/comments/commentRss/132991.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/HuzefaKatwara/services/trackbacks/132991.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Delegates and Events in C# / .NET </title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/HuzefaKatwara/archive/2009/06/23/delegates-and-events-in-c--.net.aspx" />
        <id>http://geekswithblogs.net/HuzefaKatwara/archive/2009/06/23/delegates-and-events-in-c--.net.aspx</id>
        <published>2009-06-23T01:14:40-05:00:00</published>
        <updated>2009-06-23T02:28:31Z</updated>
        <content type="html">&lt;h1&gt;&lt;span lang="en-us"&gt;&lt;a name="Overview"&gt;Overview&lt;/a&gt;&lt;/span&gt; &lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;All of us have been exposed to &lt;strong&gt;event driven&lt;/strong&gt; programming of some sort or the other. C# adds on value to the often mentioned world of event driven programming by adding support through &lt;strong&gt;events and delegates&lt;/strong&gt;. The emphasis of this article would be to identify what exactly happens when you add an event handler to your common UI controls. A simple simulation of what could possibly be going on behind the scenes when the AddOnClick or any similar event is added to the Button class will be explained. This will help you understand better the nature of event handling using multi cast delegates.&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;&lt;span lang="en-us"&gt;&lt;a name="Delegates"&gt;Delegates&lt;/a&gt;&lt;/span&gt; &lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="big"&gt;&lt;span lang="en-us"&gt;&lt;a name="Call a Function directly - No Delegate"&gt;Call a Function directly - No Delegate&lt;/a&gt;&lt;/span&gt; &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;In most cases, when we call a function, we specify the function to be called directly. If the class &lt;font color="#ff00ff"&gt;MyClass&lt;/font&gt; has a function named &lt;font color="#34b389"&gt;Process&lt;/font&gt;, we'd normally call it like this (&lt;font face="Courier New"&gt;SimpleSample.cs&lt;/font&gt;):&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;using System;&lt;br /&gt;
 &lt;br /&gt;
namespace Akadia.NoDelegate&lt;br /&gt;
{&lt;br /&gt;
    public class &lt;font color="#ff00ff"&gt;MyClass&lt;/font&gt;&lt;br /&gt;
    {&lt;br /&gt;
        public void &lt;font color="#34b389"&gt;Process()&lt;/font&gt;&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine("Process() begin");&lt;br /&gt;
            Console.WriteLine("Process() end");&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public class Test&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            &lt;font color="#ff00ff"&gt;MyClass&lt;/font&gt; myClass = new MyClass();&lt;br /&gt;
            myClass.&lt;font color="#34b389"&gt;Process()&lt;/font&gt;;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;That works well in most situations. Sometimes, however, we don't want to call a function directly - we'd like to be able to pass it to somebody else so that they can call it. This is especially useful in an event-driven system such as a graphical user interface, when I want some code to be executed when the user clicks on a button, or when I want to log some information but can't specify how it is logged.&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="big"&gt;&lt;span lang="en-us"&gt;&lt;a name="The very basic Delegate"&gt;The very basic Delegate&lt;/a&gt;&lt;/span&gt; &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references. Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;The signature of a single cast delegate is shown below:&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;&lt;strong&gt;delegate result-type identifier ([parameters]);&lt;/strong&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;where:&lt;/span&gt; &lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;span lang="en-us"&gt;result-type: The result type, which matches the return type of the function.&lt;/span&gt; &lt;/li&gt;
    &lt;li&gt;&lt;span lang="en-us"&gt;identifier: The delegate name.&lt;/span&gt; &lt;/li&gt;
    &lt;li&gt;&lt;span lang="en-us"&gt;parameters: The Parameters, that the function takes.&lt;/span&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;Examples:&lt;/span&gt; &lt;/p&gt;
&lt;table id="AutoNumber1" style="BORDER-COLLAPSE: collapse" bordercolor="#111111" cellspacing="5" cellpadding="4" width="90%" bgcolor="#cccccc" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td width="100%"&gt;
            &lt;p class="courier"&gt;&lt;strong&gt;&lt;span lang="en-us"&gt;public delegate void SimpleDelegate ()&lt;/span&gt;&lt;/strong&gt; &lt;/p&gt;
            &lt;p class="normal"&gt;&lt;span lang="en-us"&gt;This declaration defines a delegate named &lt;font face="Courier New"&gt;SimpleDelegate&lt;/font&gt;, which will encapsulate any method that takes&lt;br /&gt;
            no parameters and returns no value.&lt;/span&gt; &lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="100%"&gt;
            &lt;p class="courier"&gt;&lt;strong&gt;&lt;span lang="en-us"&gt;public delegate int ButtonClickHandler (object obj1, object obj2)&lt;/span&gt;&lt;/strong&gt; &lt;/p&gt;
            &lt;p class="normal"&gt;&lt;span lang="en-us"&gt;This declaration defines a delegate named&lt;/span&gt; &lt;font face="Courier New"&gt;&lt;span lang="en-us"&gt;ButtonClickHandler&lt;/span&gt;&lt;/font&gt;&lt;span lang="en-us"&gt;, which will encapsulate any method that takes&lt;br /&gt;
            two objects as parameters and returns an int.&lt;/span&gt; &lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;&lt;strong&gt;A delegate will allow us to specify what the function we'll be calling &lt;em&gt;looks like&lt;/em&gt; without having to specify &lt;em&gt;which&lt;/em&gt; function to call&lt;/strong&gt;. The declaration for a delegate looks just like the declaration for a function, except that in this case, we're declaring the signature of functions that this delegate can reference.&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;There are three steps in defining and using delegates:&lt;/span&gt; &lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;span lang="en-us"&gt;Declaration&lt;/span&gt; &lt;/li&gt;
    &lt;li&gt;&lt;span lang="en-us"&gt;Instantiation&lt;/span&gt; &lt;/li&gt;
    &lt;li&gt;&lt;span lang="en-us"&gt;Invocation&lt;/span&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;A very basic example (&lt;font face="Courier New"&gt;SimpleDelegate1.cs&lt;/font&gt;):&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;using System;&lt;br /&gt;
&lt;br /&gt;
namespace Akadia.BasicDelegate&lt;br /&gt;
{&lt;br /&gt;
    &lt;font color="#0000ff"&gt;// Declaration&lt;/font&gt;&lt;br /&gt;
    &lt;strong&gt;public delegate void &lt;font color="#ff00ff"&gt;SimpleDelegate&lt;/font&gt;();&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
    class TestDelegate&lt;br /&gt;
    {&lt;br /&gt;
        public static void &lt;strong&gt;&lt;font color="#008000"&gt;MyFunc&lt;/font&gt;&lt;/strong&gt;()&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine("I was called by delegate ...");&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public static void Main()&lt;br /&gt;
        {&lt;br /&gt;
            &lt;font color="#0000ff"&gt;// Instantiation&lt;/font&gt;&lt;br /&gt;
            &lt;strong&gt;&lt;font color="#ff00ff"&gt;SimpleDelegate&lt;/font&gt; simpleDelegate = new &lt;font color="#ff00ff"&gt;SimpleDelegate&lt;/font&gt;(&lt;font color="#008000"&gt;MyFunc&lt;/font&gt;);&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
            &lt;font color="#0000ff"&gt;// Invocation&lt;/font&gt;&lt;br /&gt;
            &lt;strong&gt;simpleDelegate();&lt;/strong&gt;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;/span&gt; &lt;/p&gt;
&lt;p class="normal"&gt;&lt;span lang="en-us"&gt;Compile an test:&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;# csc &lt;font face="Courier New"&gt;SimpleDelegate1.cs&lt;br /&gt;
# SimpleDelegate1.exe&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;I was called by delegate ...&lt;/font&gt;&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="big"&gt;&lt;span lang="en-us"&gt;&lt;a name="Calling Static Functions"&gt;Calling Static Functions&lt;/a&gt;&lt;/span&gt; &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;For our next, more advanced example (&lt;font face="Courier New"&gt;SimpleDelegate2.cs&lt;/font&gt;), declares a delegate that takes a single string parameter and has no return type:&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;using System;&lt;br /&gt;
&lt;br /&gt;
namespace Akadia.SimpleDelegate&lt;br /&gt;
{&lt;br /&gt;
    &lt;font color="#0000ff"&gt;// Delegate Specification&lt;/font&gt;&lt;br /&gt;
    public class MyClass&lt;br /&gt;
    {&lt;br /&gt;
        &lt;font color="#0000ff"&gt;// Declare a delegate that takes a single string parameter&lt;br /&gt;
        // and has no return type.&lt;/font&gt;&lt;br /&gt;
        &lt;strong&gt;public delegate void &lt;font color="#34b389"&gt;LogHandler&lt;/font&gt;(string message);&lt;br /&gt;
&lt;/strong&gt;&lt;br /&gt;
        &lt;font color="#0000ff"&gt;// The use of the delegate is just like calling a function directly,&lt;br /&gt;
        // though we need to add a check to see if the delegate is null&lt;br /&gt;
        // (that is, not pointing to a function) before calling the function.&lt;/font&gt;&lt;br /&gt;
        public void Process(&lt;strong&gt;&lt;font color="#34b389"&gt;LogHandler&lt;/font&gt;&lt;/strong&gt; logHandler)&lt;br /&gt;
        {&lt;br /&gt;
            if (logHandler != null)&lt;br /&gt;
            {&lt;br /&gt;
                logHandler("Process() begin");&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            if (logHandler != null)&lt;br /&gt;
            {&lt;br /&gt;
                logHandler ("Process() end");&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    &lt;font color="#0000ff"&gt;// Test Application to use the defined Delegate&lt;/font&gt;&lt;br /&gt;
    public class TestApplication&lt;br /&gt;
    {&lt;br /&gt;
       &lt;/span&gt; &lt;font color="#0000ff"&gt;&lt;span lang="en-us"&gt;// &lt;strong&gt;Static Function&lt;/strong&gt;: To which is used in the Delegate. To call the Process()&lt;br /&gt;
        // function, we need to declare a logging function: Logger() that matches&lt;br /&gt;
        // the signature of the delegate.&lt;/span&gt;&lt;/font&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
        &lt;strong&gt;static void &lt;font color="#ff00ff"&gt;Logger&lt;/font&gt;(string s)&lt;/strong&gt;&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(s);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            MyClass myClass = new MyClass();&lt;br /&gt;
&lt;br /&gt;
            &lt;font color="#0000ff"&gt;// Crate an instance of the delegate, pointing to the logging function.&lt;br /&gt;
            // This delegate will then be passed to the Process() function.&lt;/font&gt;&lt;br /&gt;
            MyClass.&lt;strong&gt;&lt;font color="#34b389"&gt;LogHandler&lt;/font&gt;&lt;/strong&gt; myLogger = new MyClass.&lt;strong&gt;&lt;font color="#34b389"&gt;LogHandler&lt;/font&gt;&lt;/strong&gt;(&lt;strong&gt;&lt;font color="#ff00ff"&gt;Logger&lt;/font&gt;&lt;/strong&gt;);&lt;br /&gt;
            myClass.Process(&lt;strong&gt;&lt;font color="#ff00ff"&gt;myLogger&lt;/font&gt;&lt;/strong&gt;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;/span&gt; &lt;/p&gt;
&lt;p class="normal"&gt;&lt;span lang="en-us"&gt;Compile an test:&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;# csc &lt;font face="Courier New"&gt;SimpleDelegate2.cs&lt;br /&gt;
# SimpleDelegate2.exe&lt;br /&gt;
&lt;font color="#0000ff"&gt;Process() begin&lt;br /&gt;
Process() end&lt;/font&gt;&lt;/font&gt;&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="big"&gt;&lt;span lang="en-us"&gt;&lt;a name="Calling Member Functions"&gt;Calling Member Functions&lt;/a&gt;&lt;/span&gt; &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;In the simple example above, the&lt;/span&gt; &lt;strong&gt;&lt;font color="#ff00ff"&gt;&lt;span lang="en-us"&gt;Logger( )&lt;/span&gt;&lt;/font&gt;&lt;/strong&gt; &lt;span lang="en-us"&gt;function merely writes the string out. A different function might want to log the information to a file, but to do this, the function needs to know what file to write the information to (&lt;font face="Courier New"&gt;SimpleDelegate3.cs&lt;/font&gt;)&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
&lt;br /&gt;
namespace Akadia.SimpleDelegate&lt;br /&gt;
{&lt;br /&gt;
    &lt;font color="#0000ff"&gt;// Delegate Specification&lt;/font&gt;&lt;br /&gt;
    public class MyClass&lt;br /&gt;
    {&lt;br /&gt;
        &lt;font color="#0000ff"&gt;// Declare a delegate that takes a single string parameter&lt;br /&gt;
        // and has no return type.&lt;/font&gt;&lt;br /&gt;
        &lt;strong&gt;public delegate void &lt;font color="#34b389"&gt;LogHandler&lt;/font&gt;(string message);&lt;br /&gt;
&lt;/strong&gt;&lt;br /&gt;
        &lt;font color="#0000ff"&gt;// The use of the delegate is just like calling a function directly,&lt;br /&gt;
        // though we need to add a check to see if the delegate is null&lt;br /&gt;
        // (that is, not pointing to a function) before calling the function.&lt;/font&gt;&lt;br /&gt;
        public void Process(&lt;font color="#34b389"&gt;&lt;strong&gt;LogHandler&lt;/strong&gt;&lt;/font&gt; logHandler)&lt;br /&gt;
        {&lt;br /&gt;
            if (logHandler != null)&lt;br /&gt;
            {&lt;br /&gt;
                logHandler("Process() begin");&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            if (logHandler != null)&lt;br /&gt;
            {&lt;br /&gt;
                logHandler ("Process() end");&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    &lt;font color="#0000ff"&gt;// The FileLogger class merely encapsulates the file I/O&lt;/font&gt;&lt;br /&gt;
    &lt;strong&gt;public class FileLogger&lt;/strong&gt;&lt;br /&gt;
    {&lt;br /&gt;
        FileStream fileStream;&lt;br /&gt;
        StreamWriter streamWriter;&lt;br /&gt;
&lt;br /&gt;
        &lt;font color="#0000ff"&gt;// Constructor&lt;/font&gt;&lt;br /&gt;
        public FileLogger(string filename)&lt;br /&gt;
        {&lt;br /&gt;
            fileStream = new FileStream(filename, FileMode.Create);&lt;br /&gt;
            streamWriter = new StreamWriter(fileStream);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        &lt;font color="#0000ff"&gt;// &lt;strong&gt;Member Function&lt;/strong&gt; which is used in the Delegate&lt;/font&gt;&lt;br /&gt;
        public void &lt;strong&gt;&lt;font color="#ff00ff"&gt;Logger&lt;/font&gt;&lt;/strong&gt;(string s)&lt;br /&gt;
        {&lt;br /&gt;
            streamWriter.WriteLine(s);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public void Close()&lt;br /&gt;
        {&lt;br /&gt;
            streamWriter.Close();&lt;br /&gt;
            fileStream.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    &lt;font color="#0000ff"&gt;// Main() is modified so that the delegate points to the Logger()&lt;br /&gt;
    // function on the fl instance of a FileLogger. When this delegate&lt;br /&gt;
    // is invoked from Process(), the member function is called and&lt;br /&gt;
    // the string is logged to the appropriate file.&lt;/font&gt;&lt;br /&gt;
    public class TestApplication&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            FileLogger fl = new FileLogger("process.log");&lt;br /&gt;
&lt;br /&gt;
            MyClass myClass = new MyClass();&lt;br /&gt;
&lt;br /&gt;
            &lt;font color="#0000ff"&gt;// Crate an instance of the delegate, pointing to the Logger()&lt;br /&gt;
            // function on the fl instance of a FileLogger.&lt;/font&gt;&lt;br /&gt;
            MyClass.&lt;strong&gt;&lt;font color="#34b389"&gt;LogHandler&lt;/font&gt;&lt;/strong&gt; myLogger = new MyClass.LogHandler(fl.&lt;strong&gt;&lt;font color="#ff00ff"&gt;Logger&lt;/font&gt;&lt;/strong&gt;);&lt;br /&gt;
            myClass.Process(myLogger);&lt;br /&gt;
            fl.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;The cool part here is that we didn't have to change the Process() function; the code to all the delegate is the same regardless of whether it refers to a &lt;strong&gt;static&lt;/strong&gt; or &lt;strong&gt;member&lt;/strong&gt; function.&lt;/span&gt; &lt;/p&gt;
&lt;p class="normal"&gt;&lt;span lang="en-us"&gt;Compile an test:&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;# csc &lt;font face="Courier New"&gt;SimpleDelegate3.cs&lt;br /&gt;
# SimpleDelegate3.exe&lt;br /&gt;
# cat process.log&lt;font color="#0000ff"&gt;&lt;br /&gt;
Process() begin&lt;br /&gt;
Process() end&lt;/font&gt;&lt;/font&gt;&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="big"&gt;&lt;span lang="en-us"&gt;&lt;a name="Multicasting"&gt;Multicasting&lt;/a&gt;&lt;/span&gt; &lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="normal"&gt;&lt;span lang="en-us"&gt;Being able to point to member functions is nice, but there are more tricks you can do with delegates. In C#, delegates are &lt;strong&gt;&lt;em&gt;multicast&lt;/em&gt;&lt;/strong&gt;, which means that they can &lt;strong&gt;point to more than one function at a time&lt;/strong&gt; (that is, they're based off the System.MulticastDelegate type). A multicast delegate maintains a list of functions that will all be called when the delegate is invoked. We can add back in the logging function from the first example, and call both delegates. Here's what the code looks like:&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
&lt;br /&gt;
namespace Akadia.SimpleDelegate&lt;br /&gt;
{&lt;br /&gt;
    // Delegate Specification&lt;br /&gt;
    &lt;strong&gt;public class MyClass&lt;/strong&gt;&lt;br /&gt;
    {&lt;br /&gt;
        &lt;font color="#0000ff"&gt;// Declare a delegate that takes a single string parameter&lt;br /&gt;
        // and has no return type.&lt;/font&gt;&lt;br /&gt;
        public delegate void &lt;strong&gt;&lt;font color="#34b389"&gt;LogHandler&lt;/font&gt;&lt;/strong&gt;(string message);&lt;br /&gt;
&lt;br /&gt;
        &lt;font color="#0000ff"&gt;// The use of the delegate is just like calling a function directly,&lt;br /&gt;
        // though we need to add a check to see if the delegate is null&lt;br /&gt;
        // (that is, not pointing to a function) before calling the function.&lt;/font&gt;&lt;br /&gt;
        &lt;strong&gt;public void Process(&lt;font color="#34b389"&gt;LogHandler&lt;/font&gt;&lt;/strong&gt; &lt;span lang="en-us"&gt;logHandler)&lt;/span&gt;&lt;/span&gt;&lt;span lang="en-us"&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
        {&lt;br /&gt;
            if (logHandler != null)&lt;br /&gt;
            {&lt;br /&gt;
                logHandler("Process() begin");&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            if (logHandler != null)&lt;br /&gt;
            {&lt;br /&gt;
                logHandler ("Process() end");&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    &lt;font color="#0000ff"&gt;// The FileLogger class merely encapsulates the file I/O&lt;/font&gt;&lt;br /&gt;
    &lt;strong&gt;public class FileLogger&lt;/strong&gt;&lt;br /&gt;
    {&lt;br /&gt;
        FileStream fileStream;&lt;br /&gt;
        StreamWriter streamWriter;&lt;br /&gt;
&lt;br /&gt;
        &lt;font color="#0000ff"&gt;// Constructor&lt;/font&gt;&lt;br /&gt;
        public FileLogger(string filename)&lt;br /&gt;
        {&lt;br /&gt;
            fileStream = new FileStream(filename, FileMode.Create);&lt;br /&gt;
            streamWriter = new StreamWriter(fileStream);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        &lt;strong&gt;&lt;font color="#0000ff"&gt;// Member Function which is used in the Delegate&lt;/font&gt;&lt;/strong&gt;&lt;br /&gt;
        &lt;strong&gt;public void Logger(string s)&lt;br /&gt;
        {&lt;br /&gt;
            streamWriter.WriteLine(s);&lt;br /&gt;
        }&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
        public void Close()&lt;br /&gt;
        {&lt;br /&gt;
            streamWriter.Close();&lt;br /&gt;
            fileStream.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
   &lt;/span&gt;&lt;/span&gt; &lt;font color="#0000ff"&gt;&lt;span lang="en-us"&gt;// Test Application which calls both Delegates&lt;/span&gt;&lt;/font&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
    public class TestApplication&lt;br /&gt;
    {&lt;br /&gt;
       &lt;strong&gt; &lt;/strong&gt;&lt;/span&gt; &lt;strong&gt;&lt;font color="#0000ff"&gt;&lt;span lang="en-us"&gt;// Static Function which is used in the Delegate&lt;/span&gt;&lt;/font&gt;&lt;/strong&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
        &lt;strong&gt;static void Logger(string s)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(s);&lt;br /&gt;
        }&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            FileLogger fl = new FileLogger("process.log");&lt;br /&gt;
&lt;br /&gt;
            MyClass myClass = new MyClass();&lt;br /&gt;
&lt;br /&gt;
            &lt;strong&gt;&lt;font color="#0000ff"&gt;// Crate an instance of the delegates, pointing to the static&lt;br /&gt;
            // Logger() function defined in the TestApplication class and&lt;br /&gt;
            // then to member function on the fl instance of a FileLogger.&lt;/font&gt;&lt;/strong&gt;&lt;br /&gt;
            &lt;strong&gt;MyClass.LogHandler myLogger = null;&lt;br /&gt;
            myLogger &lt;font color="#ff0000"&gt;+=&lt;/font&gt; new MyClass.LogHandler(Logger);&lt;br /&gt;
            myLogger &lt;font color="#ff0000"&gt;+=&lt;/font&gt; new MyClass.LogHandler(fl.Logger);&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
            myClass.Process(myLogger);&lt;br /&gt;
            fl.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;/span&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p class="normal"&gt;&lt;span lang="en-us"&gt;Compile an test:&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;# csc &lt;font face="Courier New"&gt;SimpleDelegate4.cs&lt;br /&gt;
# SimpleDelegate4.exe&lt;br /&gt;
&lt;font color="#0000ff"&gt;Process() begin&lt;br /&gt;
Process() end&lt;/font&gt;&lt;br /&gt;
# cat process.log&lt;font color="#0000ff"&gt;&lt;br /&gt;
Process() begin&lt;br /&gt;
Process() end&lt;/font&gt;&lt;/font&gt;&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;&lt;span lang="en-us"&gt;&lt;a name="Events"&gt;Events&lt;/a&gt;&lt;/span&gt; &lt;/h1&gt;
&lt;blockquote&gt;
&lt;p class="normal"&gt;&lt;span lang="en-us"&gt;The Event model in C# finds its roots in the event programming model that is popular in asynchronous programming. The basic foundation behind this programming model is the idea of "publisher and subscribers." In this model, you have &lt;strong&gt;&lt;em&gt;publishers&lt;/em&gt;&lt;/strong&gt; who will do some logic and publish an "event." Publishers will then send out their event only to &lt;strong&gt;&lt;em&gt;subscribers&lt;/em&gt;&lt;/strong&gt; who have subscribed to receive the specific event.&lt;/span&gt; &lt;/p&gt;
&lt;p class="normal"&gt;&lt;span lang="en-us"&gt;In C#, any object can &lt;em&gt;publish&lt;/em&gt; a set of events to which other applications can &lt;em&gt;subscribe&lt;/em&gt;. When the publishing class raises an event, all the subscribed applications are notified. The following figure shows this mechanism.&lt;/span&gt; &lt;/p&gt;
&lt;p class="big"&gt;&lt;span lang="en-us"&gt;&lt;img height="502" alt="" width="520" border="0" src="http://www.akadia.com/img/dotnet_user_control_1.gif" /&gt;&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="big"&gt;&lt;span lang="en-us"&gt;&lt;a name="Conventions"&gt;Conventions&lt;/a&gt;&lt;/span&gt; &lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="normal"&gt;&lt;span lang="en-us"&gt;The following important conventions are used with events&lt;/span&gt;&lt;span lang="de-ch"&gt;:&lt;/span&gt; &lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;span lang="en-us"&gt;Event Handlers in the .NET Framework return void and take two parameters.&lt;/span&gt; &lt;/li&gt;
    &lt;li&gt;&lt;span lang="en-us"&gt;The first paramter is the source of the event; that is the publishing object.&lt;/span&gt; &lt;/li&gt;
    &lt;li&gt;&lt;span lang="en-us"&gt;The second parameter is an object derived from EventArgs.&lt;/span&gt; &lt;/li&gt;
    &lt;li&gt;&lt;span lang="en-us"&gt;Events are properties of the class publishing the event.&lt;/span&gt; &lt;/li&gt;
    &lt;li&gt;&lt;span lang="en-us"&gt;The keyword event controls how the event property is accessed by the subscribing classes.&lt;/span&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p class="big"&gt;&lt;span lang="en-us"&gt;&lt;a name="Simple Event"&gt;Simple Event&lt;/a&gt;&lt;/span&gt; &lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="normal"&gt;&lt;span lang="en-us"&gt;Let's modify our logging example from above to use an event rather than a delegate:&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
&lt;br /&gt;
namespace Akadia.SimpleEvent&lt;br /&gt;
{&lt;br /&gt;
   &lt;/span&gt; &lt;strong&gt;&lt;font color="#0000ff"&gt;&lt;span lang="en-us"&gt;/* ========= Publisher of the Event ============== */&lt;/span&gt;&lt;/font&gt;&lt;/strong&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
    public class MyClass&lt;br /&gt;
    {&lt;br /&gt;
        &lt;font color="#0000ff"&gt;// Define a delegate named LogHandler, which will encapsulate&lt;br /&gt;
        // any method that takes a string as the parameter and returns no value&lt;/font&gt;&lt;br /&gt;
        public delegate void &lt;strong&gt;&lt;font color="#008000"&gt;LogHandler&lt;/font&gt;&lt;/strong&gt;(string message);&lt;br /&gt;
&lt;/span&gt;&lt;font color="#0000ff"&gt;&lt;strong&gt;&lt;span lang="en-us"&gt; &lt;br /&gt;
       &lt;/span&gt;&lt;/strong&gt; &lt;span lang="en-us"&gt;// Define an Event based on the above Delegate&lt;/span&gt;&lt;/font&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
       &lt;/span&gt; &lt;strong&gt;&lt;span lang="en-us"&gt;public event &lt;font color="#008000"&gt;LogHandler&lt;/font&gt; &lt;font color="#ff00ff"&gt;Log&lt;/font&gt;;&lt;br /&gt;
 &lt;/span&gt;&lt;/strong&gt; &lt;span lang="en-us"&gt;&lt;br /&gt;
        &lt;font color="#0000ff"&gt;// Instead of having the Process() function take a delegate&lt;br /&gt;
        // as a parameter, we've declared a Log event. Call the Event,&lt;br /&gt;
        // using the OnXXXX Method, where XXXX is the name of the Event.&lt;/font&gt;&lt;br /&gt;
        public void Process()&lt;br /&gt;
        {&lt;br /&gt;
            &lt;font color="#ff0000"&gt;&lt;strong&gt;OnLog&lt;/strong&gt;&lt;/font&gt;("Process() begin");&lt;strong&gt;&lt;br /&gt;
            &lt;font color="#ff0000"&gt;OnLog&lt;/font&gt;&lt;/strong&gt;("Process() end");&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
        &lt;font color="#0000ff"&gt;// By Default, create an OnXXXX Method, to call the Event&lt;/font&gt;&lt;br /&gt;
        &lt;strong&gt;protected void &lt;font color="#ff0000"&gt;OnLog&lt;/font&gt;(string message)&lt;br /&gt;
        {&lt;br /&gt;
            if (&lt;font color="#ff00ff"&gt;Log&lt;/font&gt; != null)&lt;br /&gt;
&lt;/strong&gt;&lt;span lang="de-ch"&gt;            {&lt;/span&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
                &lt;font color="#ff00ff"&gt;Log&lt;/font&gt;(message);&lt;br /&gt;
&lt;/span&gt;&lt;span lang="de-ch"&gt;            }&lt;/span&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
        }&lt;/span&gt;&lt;/span&gt;&lt;span lang="en-us"&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   &lt;font color="#0000ff"&gt; // The FileLogger class merely encapsulates the file I/O&lt;/font&gt;&lt;br /&gt;
    public class FileLogger&lt;br /&gt;
    {&lt;br /&gt;
        FileStream fileStream;&lt;br /&gt;
        StreamWriter streamWriter;&lt;br /&gt;
 &lt;br /&gt;
        &lt;font color="#0000ff"&gt;// Constructor&lt;/font&gt;&lt;br /&gt;
        public FileLogger(string filename)&lt;br /&gt;
        {&lt;br /&gt;
            fileStream = new FileStream(filename, FileMode.Create);&lt;br /&gt;
            streamWriter = new StreamWriter(fileStream);&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
        &lt;font color="#0000ff"&gt;// Member Function which is used in the Delegate&lt;/font&gt;&lt;br /&gt;
        public void Logger(string s)&lt;br /&gt;
        {&lt;br /&gt;
            streamWriter.WriteLine(s);&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
        public void Close()&lt;br /&gt;
        {&lt;br /&gt;
            streamWriter.Close();&lt;br /&gt;
            fileStream.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;font color="#0000ff"&gt;&lt;span lang="en-us"&gt;    /* ========= Subscriber of the Event ============== */&lt;/span&gt;&lt;/font&gt;&lt;/strong&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
   &lt;/span&gt; &lt;font color="#0000ff"&gt;&lt;span lang="en-us"&gt;// It's now easier and cleaner to merely add instances&lt;br /&gt;
    // of the delegate to the event, instead of having to&lt;br /&gt;
    // manage things ourselves&lt;/span&gt;&lt;/font&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
    public class TestApplication&lt;br /&gt;
    {&lt;br /&gt;
        static void Logger(string s)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(s);&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            FileLogger fl = new FileLogger("process.log");&lt;br /&gt;
            MyClass myClass = new MyClass();&lt;br /&gt;
 &lt;br /&gt;
&lt;/span&gt;&lt;span lang="de-ch"&gt;            &lt;font color="#0000ff"&gt;// Subscribe the Functions Logger and fl.Logger&lt;/font&gt;&lt;/span&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
            &lt;strong&gt;myClass.Log += new MyClass.LogHandler(Logger);&lt;br /&gt;
            myClass.Log += new MyClass.LogHandler(fl.Logger);&lt;br /&gt;
&lt;br /&gt;
&lt;/strong&gt;&lt;/span&gt;&lt;span lang="de-ch"&gt;&lt;strong&gt;           &lt;/strong&gt; &lt;font color="#0000ff"&gt;// The Event will now be triggered in the Process() Method&lt;/font&gt;&lt;/span&gt;&lt;strong&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
            myClass.Process();&lt;/span&gt;&lt;/strong&gt;&lt;span lang="en-us"&gt;&lt;span lang="en-us"&gt;&lt;br /&gt;
 &lt;br /&gt;
            fl.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p class="normal"&gt;&lt;span lang="en-us"&gt;Compile an test:&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;# csc &lt;font face="Courier New"&gt;SimpleEvent.cs&lt;br /&gt;
# SimpleEvent.exe&lt;br /&gt;
&lt;font color="#0000ff"&gt;Process() begin&lt;br /&gt;
Process() end&lt;/font&gt;&lt;br /&gt;
# cat process.log&lt;font color="#0000ff"&gt;&lt;br /&gt;
Process() begin&lt;br /&gt;
Process() end&lt;/font&gt;&lt;/font&gt;&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="big"&gt;&lt;span lang="en-us"&gt;&lt;a name="The Second Change Event Example"&gt;The Second Change Event Example&lt;/a&gt;&lt;/span&gt; &lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="normal"&gt;&lt;span lang="en-us"&gt;Suppose you want to create a Clock class that uses events to notify potential subscribers whenever the local time changes value by one second. Here is the complete, documented example:&lt;/span&gt; &lt;/p&gt;
&lt;p class="courier"&gt;&lt;span lang="en-us"&gt;using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
&lt;br /&gt;
namespace SecondChangeEvent&lt;br /&gt;
{&lt;br /&gt;
   &lt;strong&gt;&lt;font color="#0000ff"&gt;/* ======================= Event Publisher =============================== */&lt;/font&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
   &lt;font color="#0000ff"&gt;// Our subject -- it is this class that other classes&lt;br /&gt;
   // will observe. This class publishes one event:&lt;br /&gt;
   // SecondChange. The observers subscribe to that event.&lt;/font&gt;&lt;br /&gt;
   public class Clock&lt;br /&gt;
   {&lt;br /&gt;
     &lt;font color="#0000ff"&gt; // Private Fields holding the hour, minute and second&lt;/font&gt;&lt;br /&gt;
      private int _hour;&lt;br /&gt;
      private int _minute;&lt;br /&gt;
      private int _second;&lt;br /&gt;
&lt;br /&gt;
     &lt;font color="#0000ff"&gt; // The delegate named SecondChangeHandler, which will encapsulate&lt;br /&gt;
      // any method that takes a clock object and a TimeInfoEventArgs&lt;br /&gt;
      // object as the parameter and returns no value. It's the&lt;br /&gt;
      // delegate the subscribers must implement.&lt;/font&gt;&lt;br /&gt;
      &lt;strong&gt;public delegate void SecondChangeHandler (&lt;br /&gt;
         object clock,&lt;br /&gt;
         TimeInfoEventArgs timeInformation&lt;br /&gt;
      );&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
      &lt;font color="#0000ff"&gt;// The event we publish&lt;/font&gt;&lt;br /&gt;
      &lt;strong&gt;public event SecondChangeHandler SecondChange;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
     &lt;font color="#0000ff"&gt; // The method which fires the Event&lt;/font&gt;&lt;br /&gt;
      &lt;strong&gt;protected void OnSecondChange(&lt;br /&gt;
         object clock,&lt;br /&gt;
         TimeInfoEventArgs timeInformation&lt;br /&gt;
      )&lt;br /&gt;
      {&lt;br /&gt;
         // Check if there are any Subscribers&lt;br /&gt;
         if (SecondChange != null)&lt;br /&gt;
         {&lt;br /&gt;
            // Call the Event&lt;br /&gt;
            SecondChange(clock,timeInformation);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
     &lt;font color="#0000ff"&gt; // Set the clock running, it will raise an&lt;br /&gt;
      // event for each new second&lt;/font&gt;&lt;br /&gt;
      public void Run()&lt;br /&gt;
      {&lt;br /&gt;
         for(;;)&lt;br /&gt;
         {&lt;br /&gt;
            &lt;font color="#0000ff"&gt;// Sleep 1 Second&lt;/font&gt;&lt;br /&gt;
            Thread.Sleep(1000);&lt;br /&gt;
&lt;br /&gt;
            &lt;font color="#0000ff"&gt;// Get the current time&lt;/font&gt;&lt;br /&gt;
            System.DateTime dt = System.DateTime.Now;&lt;br /&gt;
&lt;br /&gt;
            &lt;font color="#0000ff"&gt;// If the second has changed&lt;br /&gt;
            // notify the subscribers&lt;/font&gt;&lt;br /&gt;
            if (dt.Second != _second)&lt;br /&gt;
            {&lt;br /&gt;
               &lt;font color="#0000ff"&gt;// Create the TimeInfoEventArgs object&lt;br /&gt;
               // to pass to the subscribers&lt;/font&gt;&lt;br /&gt;
               TimeInfoEventArgs timeInformation =&lt;br /&gt;
                  new TimeInfoEventArgs(&lt;br /&gt;
                  dt.Hour,dt.Minute,dt.Second);&lt;br /&gt;
&lt;br /&gt;
               &lt;font color="#0000ff"&gt;// If anyone has subscribed, notify them&lt;/font&gt;&lt;br /&gt;
               &lt;strong&gt;OnSecondChange (this,timeInformation);&lt;/strong&gt;&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            &lt;font color="#0000ff"&gt;// update the state&lt;/font&gt;&lt;br /&gt;
            _second = dt.Second;&lt;br /&gt;
            _minute = dt.Minute;&lt;br /&gt;
            _hour = dt.Hour;&lt;br /&gt;
&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;font color="#0000ff"&gt;// The class to hold the information about the event&lt;br /&gt;
   // in this case it will hold only information&lt;br /&gt;
   // available in the clock class, but could hold&lt;br /&gt;
   // additional state information&lt;/font&gt;&lt;br /&gt;
   public class TimeInfoEventArgs : EventArgs&lt;br /&gt;
   {&lt;br /&gt;
      public TimeInfoEventArgs(int hour, int minute, int second)&lt;br /&gt;
      {&lt;br /&gt;
         this.hour = hour;&lt;br /&gt;
         this.minute = minute;&lt;br /&gt;
         this.second = second;&lt;br /&gt;
      }&lt;br /&gt;
      public readonly int hour;&lt;br /&gt;
      public readonly int minute;&lt;br /&gt;
      public readonly int second;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  &lt;strong&gt;&lt;font color="#0000ff"&gt; /* ======================= Event Subscribers =============================== */&lt;br /&gt;
&lt;/font&gt;&lt;/strong&gt;&lt;br /&gt;
   &lt;font color="#0000ff"&gt;// An observer. DisplayClock subscribes to the&lt;br /&gt;
   // clock's events. The job of DisplayClock is&lt;br /&gt;
   // to display the current time&lt;/font&gt;&lt;br /&gt;
   public class DisplayClock&lt;br /&gt;
   {&lt;br /&gt;
      &lt;font color="#0000ff"&gt;// Given a clock, subscribe to&lt;br /&gt;
      // its SecondChangeHandler event&lt;/font&gt;&lt;br /&gt;
      public void Subscribe(Clock theClock)&lt;br /&gt;
      {&lt;br /&gt;
         &lt;strong&gt;theClock.SecondChange +=&lt;br /&gt;
            new Clock.SecondChangeHandler(TimeHasChanged);&lt;/strong&gt;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
     &lt;font color="#0000ff"&gt; // The method that implements the&lt;br /&gt;
      // delegated functionality&lt;/font&gt;&lt;br /&gt;
      public void TimeHasChanged(&lt;br /&gt;
         object theClock, TimeInfoEventArgs ti)&lt;br /&gt;
      {&lt;br /&gt;
         Console.WriteLine("Current Time: {0}:{1}:{2}",&lt;br /&gt;
            ti.hour.ToString(),&lt;br /&gt;
            ti.minute.ToString(),&lt;br /&gt;
            ti.second.ToString());&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;font color="#0000ff"&gt;// A second subscriber whose job is to write to a file&lt;/font&gt;&lt;br /&gt;
   public class LogClock&lt;br /&gt;
   {&lt;br /&gt;
      public void Subscribe(Clock theClock)&lt;br /&gt;
      {&lt;br /&gt;
         &lt;strong&gt;theClock.SecondChange +=&lt;br /&gt;
            new Clock.SecondChangeHandler(WriteLogEntry);&lt;/strong&gt;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      &lt;font color="#0000ff"&gt;// This method should write to a file&lt;br /&gt;
      // we write to the console to see the effect&lt;/font&gt;&lt;br /&gt;
      &lt;font color="#0000ff"&gt;// this object keeps no state&lt;/font&gt;&lt;br /&gt;
      public void WriteLogEntry(&lt;br /&gt;
         object theClock, TimeInfoEventArgs ti)&lt;br /&gt;
      {&lt;br /&gt;
         Console.WriteLine("Logging to file: {0}:{1}:{2}",&lt;br /&gt;
            ti.hour.ToString(),&lt;br /&gt;
            ti.minute.ToString(),&lt;br /&gt;
            ti.second.ToString());&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;strong&gt;&lt;font color="#0000ff"&gt;/* ======================= Test Application =============================== */&lt;br /&gt;
&lt;/font&gt;&lt;/strong&gt;&lt;br /&gt;
   &lt;font color="#0000ff"&gt;// Test Application which implements the&lt;br /&gt;
   // Clock Notifier - Subscriber Sample&lt;/font&gt;&lt;br /&gt;
   public class Test&lt;br /&gt;
   {&lt;br /&gt;
      public static void Main()&lt;br /&gt;
      {&lt;br /&gt;
         &lt;font color="#0000ff"&gt;// Create a new clock&lt;/font&gt;&lt;br /&gt;
         Clock theClock = new Clock();&lt;br /&gt;
&lt;br /&gt;
         &lt;font color="#0000ff"&gt;// Create the display and tell it to&lt;br /&gt;
         // subscribe to the clock just created&lt;/font&gt;&lt;br /&gt;
         DisplayClock dc = new DisplayClock();&lt;br /&gt;
         dc.Subscribe(theClock);&lt;br /&gt;
&lt;br /&gt;
         &lt;font color="#0000ff"&gt;// Create a Log object and tell it&lt;br /&gt;
         // to subscribe to the clock&lt;/font&gt;&lt;br /&gt;
         LogClock lc = new LogClock();&lt;br /&gt;
         lc.Subscribe(theClock);&lt;br /&gt;
&lt;br /&gt;
         &lt;font color="#0000ff"&gt;// Get the clock started&lt;/font&gt;&lt;br /&gt;
         theClock.Run();&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;/span&gt;&lt;br /&gt;
} &lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;&lt;span lang="en-us"&gt;&lt;a name="Conclusion"&gt;Conclusion&lt;/a&gt;&lt;/span&gt; &lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;The Clock class from the last sample could simply print the time rather then raising an event, so &lt;strong&gt;why bother with the introduction of using delegates&lt;/strong&gt;? The advantage of the publish / subscribe idiom is that &lt;strong&gt;any number of classes can be notified when an event is raised&lt;/strong&gt;. The subscribing classes do not need to know how the Clock works, and the Clock does not need to know what they are going to do in response to the event. Similarly a button can publish an Onclick event, and any number of unrelated objects can subscribe to that event, receiving notification when the button is clicked.&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span lang="en-us"&gt;&lt;strong&gt;The publisher and the subscribers are decoupled by the delegate&lt;/strong&gt;. This is highly desirable as it makes for more flexible and robust code.&lt;/span&gt; &lt;span lang="de-ch"&gt;T&lt;/span&gt;&lt;span lang="en-us"&gt;he clock can chnage how it detects time without breaking any of the subscribing classes. The subscribing classes can ch&lt;/span&gt;&lt;span lang="de-ch"&gt;ange&lt;/span&gt; &lt;span lang="en-us"&gt;how they respond to time c&lt;/span&gt;&lt;span lang="de-ch"&gt;hanges&lt;/span&gt; &lt;span lang="en-us"&gt;without breaking the Clock. The two classes spin indepentdently of one another, which makes for code that is easier to maintain.&lt;/span&gt; &lt;/p&gt;
&lt;/blockquote&gt;&lt;img src="http://geekswithblogs.net/HuzefaKatwara/aggbug/132989.aspx" width="1" height="1" /&gt;</content>
    </entry>
</feed>