I've simplified the implementation from the methods for brevity, but functionality-wise multicasting a delegate defined with the delegate keyword will be as intuitive as multicasting with the equivalent Action or Func type.
One thing to note is that if your GreetingDelegate had a return value rather than void, making it functionally equivalent to a Func, multicasting isn't especially useful if your goal is to use the return value of each call. The default behaviour is that when you call "greetFunc" below it will execute each of the added functions in series, but the return value with be the value returned by the last function added to the multicast list, so "3".
It is possible to loop through each function individually, although that does make multicasting somewhat pointless:
foreach (Func<int> func in greetFunc.GetInvocationList()) {
Console.WriteLine(func());
}
Updated source:
using System;
namespace DelegateGreetingApp {
// 1.
delegate void GreetingDelegate();
class Program {
static void Main(string[] args) {
// 1. Multicast delegates
GreetingDelegate greetDelegate = new GreetingDelegate(GoodMorning);
greetDelegate += GoodAfternoon;
greetDelegate += GoodEvening;
greetDelegate();
Console.WriteLine("---");
// 2. Multicast delegates (using Action)
Action greetAction = GoodMorning;
greetAction += GoodAfternoon;
greetAction += GoodEvening;
greetAction();
Console.WriteLine("---");
// 3. Multicast delegates (using Func)
Func<int> greetFunc = () => {
GoodMorning();
return 1;
};
greetFunc += () => {
GoodAfternoon();
return 2;
};
greetFunc += () => {
GoodEvening();
return 3;
};
Console.WriteLine(greetFunc());
Console.Read();
}
static void GoodMorning() {
//if (DateTime.Now.ToString().EndsWith("PM"))
if (DateTime.Now.Hour > 4 && DateTime.Now.Hour < 12)
Console.WriteLine("Good Morning");
}
static void GoodAfternoon() {
//if (DateTime.Now.ToString().EndsWith("PM"))
if (DateTime.Now.Hour > 11 && DateTime.Now.Hour < 17)
Console.WriteLine("Good Afternoon");
}
static void GoodEvening() {
if (DateTime.Now.Hour > 16 || DateTime.Now.Hour < 5)
Console.WriteLine("Good Evening");
}
}
}