I readily admit that this reply doesn't compare the difference between delegates and events. However, I think with the replies that others have given for Events along with a bit more detailed explanation on delegates, you will see the difference between the two.
More specifically, once you have a clearer understanding of delegates, I think you will gain a conceptual difference between delegates and events.
What has helped me in understanding what a delegate is, is thinking of them as nothing more than a container for a single method.
To conceptualize how a delegate is a 'container' of a method, review this example that makes use of a delegate to invoke three different methods - Note that although the same delegate instance is used to invoke three different methods, the delegate instance will only contain (or target) one method at any given time.
class Program
{
delegate void MyDelegate();
static void Main()
{
//Notice how we use the same delegate instance
//to target different methods of the same signature
MyDelegate myDelegate = new MyDelegate(MethodA);
myDelegate(); //Invoke the method
myDelegate = MethodB;
myDelegate();
myDelegate = MyClass.MethodZ;
myDelegate();
Console.ReadLine();
}
static void MethodA()
{
Console.WriteLine("Method 'A' is doing work.");
}
static void MethodB()
{
Console.WriteLine("Method 'B' is doing work.");
}
class MyClass
{
public static void MethodZ()
{
Console.WriteLine("Method 'Z' of MyClass is doing work");
}
}
}
You'll notice that the delegate in the example can contain/target any method that has the same signature as the delegate (returns void and takes zero parameters in our example). If you pause here and digest that principle, you're off to a good start at understanding delegates.
With that being said, what confused me about delegates is when I would ever explicitly implement delegates in my code. The example program above explicitly makes use of a delegate, but there is no practical reason to do so since the Main() method could just as well have called the target methods directly - i.e. we could have just done...
static void Main()
{
MethodA();
MethodB();
MyClass.MethodZ();
Console.ReadLine();
}
So what is an example of when we would implement a delegate explicitly? Jon Skeet helped me with answering that question when he said
...you can think of a delegate type as
being a bit like an interface with a
single method.
Knowing that a delegate is a container for a method and, now, considering that a delegate is like an Interface with a single method, consider this:
Say we have a program that will start the engines of different types of vehicles - Car, Motorcyle, and Airplane.
Our program will consist of vehicle classes - one class for each vehicle type. Each vehicle class is responsible for starting its own engine. In addition, similar to implementing an Interface, each class will make sure that its method that starts its own engine, will have a specified signature that all other classes (and the Main) agree upon.
So we have something like this:
class VehicleProgram
{
//All vehicle classes implement their own engine starter method that has this signature
delegate void StartEngine();
static void Main()
{
//The Main doesn't know the details of starting an engine.
//It delegates the responsibility to the concrete vehicle class
foreach (StartEngine starter in GetVehicleStarters())
{
starter(); //Invoke the method
}
Console.ReadLine();
}
static List<StartEngine> GetVehicleStarters()
{
//Create a list of delegates that target the engine starter methods
List<StartEngine> starters = new List<StartEngine>();
starters.Add(Car.StartCar);
starters.Add(Motorcycle.StartMotorcycle);
starters.Add(Airplane.StartAirplane);
return (starters);
}
class Car
{
public static void StartCar()
{
Console.WriteLine("The car is starting.");
}
}
class Motorcycle
{
public static void StartMotorcycle()
{
Console.WriteLine("The motorcycle is starting.");
}
}
class Airplane
{
public static void StartAirplane()
{
Console.WriteLine("The airplane is starting.");
}
}
}
Since delegates hold methods, we were able to implement a design where the Main() simply gets a list of delegates then invokes the method. This design is very similar to how we implement Interfaces.
Read more on When to Use Delegates Instead of Interfaces