Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5.  
  6. namespace IMJunior
  7. {
  8.     class Program
  9.     {
  10.         delegate bool Predicate(string item);
  11.  
  12.         delegate string Predicate2(string item);
  13.  
  14.         static void Main()
  15.         {
  16.             string[] words = { "Hello", "Temp", "Some", "Hesus" };
  17.  
  18.             foreach (var word in Where(words, (x) => x.StartsWith("H")))
  19.             {
  20.                 Console.WriteLine(word);
  21.             }
  22.         }
  23.  
  24.         static void Foreach(string[] array, Predicate2 predicate)
  25.         {
  26.             List<string> result = new List<string>();
  27.             foreach (var item in array)
  28.             {
  29.                 result.Add(predicate(item));
  30.             }
  31.  
  32.             array = result.ToArray();
  33.         }
  34.  
  35.         static string[] Where(string[] array, Predicate predicate)
  36.         {
  37.             List<string> result = new List<string>();
  38.  
  39.             foreach (var item in array)
  40.             {
  41.                 if (predicate(item))
  42.                 {
  43.                     result.Add(item);
  44.                 }
  45.             }
  46.  
  47.             return result.ToArray();
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement