Paul Mendoza C# blog
Sunday, October 29, 2006
 
Playing with delegates

Tonight I decided to take a quick look at delegates in C#. I've been using them for quite some time but I've never really done a lot with them and although I understand how to use them, I haven't really understood what they were doing so I figured I'd do some testing with a small console application. Here is what I wrote.





using System;
using System.Collections.Generic;
using System.Text;
namespace DelegateTesting
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Pauls Delegate tester:");
//GetFunction myNewFunction = new GetFunction(WriteMe);
MyProgram program = new MyProgram();

}

}
public class MyProgram
{
public MyProgram()
{
Console.WriteLine("Got to here...");
GetFunction myNewFunction = new GetFunction(WriteMe);
myNewFunction();
myNewFunction = new GetFunction(ShitIBroke);
myNewFunction();
SecondSameDelegate secondDel = new SecondSameDelegate(myNewFunction);
secondDel();
Console.WriteLine("Now we reassign myNewFunction.");
myNewFunction = new GetFunction(WriteMe);
myNewFunction();
secondDel();
}

public void WriteMe()
{
Console.WriteLine("WriteMe function called!!");
}
public void ShitIBroke()
{
Console.WriteLine("ShitIBroke function called!");
}
public delegate void GetFunction();
public delegate void SecondSameDelegate();
}
}


The part that I thought was interesting was that the assignment of one delegate to another delegate would result in the reference function being assigned instead of the delegate itself.

Technorati Tags: , , , , , , ,

 
Comments: Post a Comment





<< Home
I am currently an ASP.NET, C# developer working on MangosteenNation.com, a XanGo website for helping people build their businesses. I am also pursuing a degree at CSU San Marcos in Southern California.

XanGo at Mangosteen Nation

Archives
October 2005 / November 2005 / December 2005 / January 2006 / February 2006 / March 2006 / April 2006 / May 2006 / June 2006 / July 2006 / August 2006 / September 2006 / October 2006 / November 2006 / December 2006 / January 2007 / April 2007 / May 2007 / June 2007 / August 2007 / February 2008 / August 2008 /


Powered by Blogger

Subscribe to
Posts [Atom]