using System; delegate void CustomDel(string s); class TestClass { static void Hello(string s){ Console.WriteLine($" Hello, {s}!"); } static void Goodbye(string s){ Console.WriteLine($" Goodbye, {s}!"); } static void Main() { CustomDel hiDel, byeDel, multiDel, multiMinusHiDel; hiDel = Hello; byeDel = Goodbye; multiDel = hiDel + byeDel; multiMinusHiDel = multiDel - hiDel; Console.WriteLine("Invoking delegate hiDel:"); hiDel("A"); Console.WriteLine("Invoking delegate byeDel:"); byeDel("B"); Console.WriteLine("Invoking delegate multiDel:"); multiDel("C"); Console.WriteLine("Invoking delegate multiMinusHiDel:"); multiMinusHiDel("D"); } } /* Output: Invoking delegate hiDel: Hello, A! Invoking delegate byeDel: Goodbye, B! Invoking delegate multiDel: Hello, C! Goodbye, C! Invoking delegate multiMinusHiDel: Goodbye, D! */
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter