Advertisement
illiden

UnionCollectionsHashSet

Jul 15th, 2022
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLight
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string[] firstArray = ReadStringArray(' ');
  11.             string[] secondArray = ReadStringArray(' ');
  12.             HashSet<string> unionCollection = new HashSet<string>();
  13.             AddRangeToCollection(unionCollection, firstArray);
  14.             AddRangeToCollection(unionCollection, secondArray);
  15.            
  16.             Console.WriteLine("Итоговая коллекция: ");
  17.             PrintCollection(unionCollection);
  18.         }
  19.        
  20.         private static void AddRangeToCollection<T>(ICollection<T> originCollection, IList<T> addedCollection)
  21.         {
  22.             foreach (T addedElement in addedCollection)
  23.             {
  24.                 originCollection.Add(addedElement);
  25.             }
  26.         }
  27.  
  28.         private static void PrintCollection<T>(ICollection<T> collection)
  29.         {
  30.             foreach (T element in collection)
  31.             {
  32.                 Console.Write($"{element} ");
  33.             }
  34.             Console.WriteLine();
  35.         }
  36.        
  37.         private static string[] ReadStringArray(char separator)
  38.         {
  39.             Console.WriteLine($"Введите массив через '{separator}'");
  40.             string text = Console.ReadLine();
  41.             string[] result = text.Split(separator);
  42.             return result;
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement