Advertisement
Ryoh

Duplicating items in a list

Apr 23rd, 2021
1,768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.61 KB | None | 0 0
  1. // Create the list of items
  2. var items = new List<Items>()
  3. items.Add("Apple")
  4. items.Add("Orange")
  5.  
  6. // This list will temporarily hold duplicates of the original items
  7. var duplicates = new List<Items>();
  8.  
  9. // Loop through the original items and add each item to the list of duplicates
  10. foreach (Items item in items)
  11. {
  12.     duplicates.Add(item);
  13. }
  14.  
  15. // Loop through the duplicates and add the item back into the original list
  16. foreach (Items item in duplicates)
  17. {
  18.     items.Add(card);
  19. }
  20.  
  21. // Print each item
  22. foreach (Items item in items)
  23. {
  24.     Console.Write($"{item}, ");
  25. }
  26.  
  27. //Output
  28. //"Apple, Orange, Apple, Orange, "
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement