C# Events Terminology

Started by
2 comments, last by Xai 14 years, 1 month ago
Hi, I've just been playing around with a simple program to define and use an event. The program works fine but it did bring up a question with regards my understanding of event terminology. Here's the program:

using System;

namespace Test
{
	public class Program
	{
		public delegate void NumberDelegate();
		public static event NumberDelegate Test;

		public static void Main()
		{
			NumberDelegate nd = One;
			nd += Two;
			nd += Three;
			nd += Four;

			Program.Test += Program_Test;
			
			foreach (NumberDelegate d in nd.GetInvocationList())
			{
				try
				{
					d();
				}

				catch (Exception e)
				{
					Console.WriteLine(e.Message);
				}
			}
		}

		public static void One()
		{
			Console.WriteLine("One().");
			throw new Exception("Exception in One().");
		}

		public static void Two()
		{
			Console.WriteLine("Two().");
		}

		public static void Three()
		{
			Console.WriteLine("Three().");
		}

		public static void Four()
		{
			Console.WriteLine("Four().");
			Console.WriteLine("Raising Program.Test event from Four().");

			OnTest();
		}

		public static void OnTest()
		{
			if (Test != null)
			{
				Console.WriteLine("Program.Test event raised.");
				Test();
			}
		}

		public static void Program_Test()
		{
			Console.WriteLine("Program.Test event handled.");
		}
	}
}

If I comment out the Program.Test += Program_Test; line, the program only reports that the event is being raised. That's fine, but I think I have my terms mixed up. Does raising an event mean you're attempting to fire it? Whilst firing the event means actually calling it? I know it's a minor thing but I was under the impression that an event is raised irregardless of having any subscribers, hence the confusion. Could someone clarify this for me please?
Advertisement
If I understand my terms correctly, raising the event is triggering it. In syntax, Test(). Firing off the event generally refers to that, and the mechanics of iterating through the observers (if any), and triggering them.

The nuance isn't usually meaningful, so the terms tend to be used interchangeably.
Thank you!
Event += Handler // this is "attaching" to an event

// all things which are "attached" to an event are said to be "listening" to the event.

obviously, Event -= Handler would be called "detaching" from the event, or ceasing to listen or respond to the event.

When the object which has the event "calls" it, via syntax in C# that looks just like calling a function, this is called "raising" the event (or also "calling" or "triggering" the event - although trigger is ambiguous because it means either the initial reason the event was raised, or the raising itself). Occasionally you even here it referred to as "invoking" ... because in C# events are implemented as delegates, and the general term for "calling" a delegate is "invoking" not "raising" ...

An event is raised regardless if there are any listeners or not - logically speaking. But of course there is no reason to expect the program / debugger to actually run any code in this case (also no reason NOT to either).

P.S. I don't think I've even seen "irregardless" written before. I've heard it verbally, just never seen it written.

Good Luck.

This topic is closed to new replies.

Advertisement