elena1234

Func and Action in C#

Dec 9th, 2021 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.68 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Delegates
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int[] numbers = new int[] { 1, 2, 3, 4, 5 };
  10.             Func<int, int> sum = x => x + 100; // except int and return int
  11.             Func<int, int> multiply = x => x * 2;
  12.             Action<int> print = x => Console.WriteLine(x); // except int, return always void
  13.  
  14.             foreach (var num in numbers)
  15.             {
  16.                 Console.WriteLine(sum(num));
  17.                 var multiplyResult = multiply(num);
  18.                 print(multiplyResult);
  19.                 Console.WriteLine("--------------------");
  20.             }
  21.         }
  22.     }
  23. }
  24.  
Add Comment
Please, Sign In to add comment