Created
January 14, 2019 06:56
-
-
Save jflopezfernandez/6ec4bee2c17ade2a49bcd39fa01ed3cb to your computer and use it in GitHub Desktop.
Action delegate and a type alias in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| namespace ActionAndFuncDelegates | |
| { | |
| using DisplayMessageAction = Action<string, ConsoleColor, int>; | |
| class Program | |
| { | |
| static void DisplayMessage(string msg, ConsoleColor txtColor, int printCount) | |
| { | |
| ConsoleColor previous = Console.ForegroundColor; | |
| Console.ForegroundColor = txtColor; | |
| for (int i = 0; i < printCount; i++) | |
| { | |
| Console.WriteLine(msg); | |
| } | |
| // Restore Color. | |
| Console.ForegroundColor = previous; | |
| } | |
| static void Main(string[] args) | |
| { | |
| Action<string, ConsoleColor, int> actionTarget = new Action<string, ConsoleColor, int>(DisplayMessage); | |
| actionTarget("First test!", ConsoleColor.Yellow, 3); | |
| actionTarget("Second", ConsoleColor.Cyan, 2); | |
| actionTarget("Third", ConsoleColor.Magenta, 4); | |
| DisplayMessageAction msgAction = new DisplayMessageAction(DisplayMessage); | |
| msgAction("First test!", ConsoleColor.Yellow, 3); | |
| msgAction("Second", ConsoleColor.Cyan, 2); | |
| msgAction("Third", ConsoleColor.Magenta, 4); | |
| Console.ReadLine(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment