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: .NET, C#, code, delegates, DOS, programming, Visual Studio, VS2005
Subscribe to
Posts [Atom]