Advertisement
elena1234

FindEvensOrOdds- how to use Func<>

Jan 25th, 2021
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace FindEvensOrOdds
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] range = Console.ReadLine().Split().Select(int.Parse).ToArray();
  12.             int lowerBound = range[0];
  13.             int upperBound = range[1];
  14.             string condition = Console.ReadLine();
  15.             Func<int, int, int[]> funcToListNumber = (lowerBound, upperBound) =>
  16.             {
  17.                 var newListWithNumbers = new List<int>();              
  18.                 for (int i = lowerBound; i <= upperBound; i++)
  19.                 {
  20.                     newListWithNumbers.Add(i);
  21.                 }
  22.  
  23.                 return newListWithNumbers.ToArray();
  24.             };
  25.  
  26.             if (condition == "odd")
  27.             {
  28.                 Console.WriteLine(string.Join(" ",funcToListNumber(lowerBound,upperBound).Where(x=>x % 2 != 0)));
  29.             }
  30.  
  31.             else if (condition == "even")
  32.             {
  33.                 Console.WriteLine(string.Join(" ", funcToListNumber(lowerBound,upperBound).Where(x=>x % 2 == 0)));
  34.             }
  35.         }
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement