Latest News
Mar 27, 2017, 7:02 AM
using System;
using System.Collections.Generic;

/* This example shows one way of using delegates in a command parser
 * The final output of this example will be
 * doHello function called.
 * doWorld function called.
 * doThere function called.
 * doWorld function called.
 *
 * Notice in this example the delegate and the associated functions don't accept any parameters.
 * Delegates don't have to have parameters, infact, they can also have a return value
 */
namespace DelegatesTutorial
{
  // Declare the delegate to be used later, this sets the return type and expected parameters
  // for the delegate. All functions to be included must have the same return type and parameter list.
  delegate void myDelegate();
  // Declare a class to contain the keyword and the delegate
  class Command
  {
    public string Keyword = default(string);
    public myDelegate CmdPointer;
    public Command(string sKeyword, myDelegate dPointer)
    {
      Keyword = sKeyword;
      CmdPointer = dPointer;
    }
  }

  class Example3
  {
    // Instead of creating a list of delegates, we'll create a list of the command class
    List<Command> Commands = new List<Command>();

    public Example3()
    {
      // We'll now add three commands
      Commands.Add(new Command("hello", new myDelegate(doHello)));
      Commands.Add(new Command("there", new myDelegate(doThere)));
      Commands.Add(new Command("world", new myDelegate(doWorld)));

      // Let's use the command parser
      CommandParser("Hello");
      CommandParser("world");
      CommandParser("there");
      CommandParser("world");
    }

    // Here's the actual command parser. It simply compares text then calls the
    // appropriate function through the delegate
    void CommandParser(string CommandToParse)
    {
      foreach (Command c in Commands)
      {
        if (c.Keyword.ToLower() == CommandToParse.ToLower())
        c.CmdPointer();
      }
    }

    // These are the functions to be called by the delegate
    void doHello()
    {
      Console.WriteLine("doHello function called.");
    }
    void doThere()
    {
      Console.WriteLine("doThere function called.");
    }
    void doWorld()
    {
      Console.WriteLine("doWorld function called.");
    }
  }
}
Mar 27, 2017, 6:57 AM
using System;
/* This example illustrates building one delegate to call several functions in sequence
* The final output of this example will be
*
* WriteOut method called
* 2
* WriteIn method called
* 1
* WriteIn method called
* 0
*
* Notice that even though we pass 1 to the arguments of the delegate, the first iteration of
* WriteIn recieves the value of 2. This is because WriteOut modified that value to 2.
* Using this method, all functions share the same argument, thus if the first function modifies
* it's value, it's modified for all the rest of the functions in the list.
* This is very important in deciding whether to use the method in example1 or example2, as it
* can significantly change the resulting output of the program.
* (NOTE: This is not true on every compiler, test to find out)
*/
namespace DelegatesTutorial
{
class Example2
{
// Declare the delegate to be used later, this sets the return type and expected parameters
// for the delegate. All functions to be included must have the same return type and parameter list.
delegate void MyDelegate(int number);
public Example2()
{
// We'll create a single delegate from MyDelegate and assign WriteOut to it
MyDelegate TheDelegate = new MyDelegate(WriteOut);
// Now add WriteIn to the existing delegate
TheDelegate += new MyDelegate(WriteIn);
// Call the functions of the delegate
TheDelegate(1);
// Remove the WriteOut delegate
TheDelegate -= new MyDelegate(WriteOut);
// Call the functions of the delegate again.
TheDelegate(1);
}
// These are the functions to be called by the delegate
void WriteOut(int number)
{
Console.WriteLine("WriteOut method called");
int i = ++number;
Console.WriteLine(i.ToString());
}
void WriteIn(int number)
{
Console.WriteLine("WriteIn method called");
int i = --number;
Console.WriteLine(i.ToString());
}
}
}