Posts

Showing posts with the label #ActionDelegate

C# Action Delegate

Image
Action   is a delegate type defined in the   System   namespace. An Action type delegate is the same as   Func delegate   except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type.  For example, the following delegate prints an int value. Example: C# Delegate public delegate void Print( int val); static void ConsolePrint( int i) { Console .WriteLine(i); } static void Main( string [] args) { Print prnt = ConsolePrint; prnt(10); } Output: 10 You can use an Action delegate instead of defining the above Print delegate, for example: Example: Action delegate static void ConsolePrint( int i) { Console .WriteLine(i); } static void Main( string [] args) { Action < int > printActionDel = ConsolePrint; printActionDel(10); } Try it You can initialize an Action delegate using the new keyword or by directly assigning a method: ...