# ICollection

***

**1. Initialize an empty ICollection**

```csharp
ICollection<int> numbers = new List<int>();
```

**2. Add elements to an ICollection**

```csharp
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
```

**3. Remove elements from an ICollection**

```csharp
numbers.Remove(1);
```

**4. Iterate over the elements in an ICollection**

```csharp
foreach (int number in numbers)
{
    Console.WriteLine(number);
}
```

**5. Check if an element is contained in an ICollection**

```csharp
if (numbers.Contains(2))
{
    Console.WriteLine("The number 2 is in the collection.");
}
```

**6. Get the number of elements in an ICollection**

```csharp
Console.WriteLine("The number of elements in the collection is: " + numbers.Count);
```

**7. Clear all elements from an ICollection**

```csharp
numbers.Clear();
```

**8. Sort the elements in an ICollection**

```csharp
numbers.Sort();
```

**9. Reverse the order of the elements in an ICollection**

```csharp
numbers.Reverse();
```

**10. Copy the elements of an ICollection to an array**

```csharp
int[] numbersArray = numbers.ToArray();
```

**11. Create a new ICollection from an existing array**

```csharp
ICollection<int> newNumbers = new List<int>(numbersArray);
```

**12. Create a HashSet from an existing ICollection**

```csharp
HashSet<int> uniqueNumbers = new HashSet<int>(numbers);
```

**13. Create a Dictionary from an existing ICollection**

```csharp
Dictionary<int, string> numberNames = numbers.ToDictionary(number => number, number => number.ToString());
```

**14. Create a Stack from an existing ICollection**

```csharp
Stack<int> numberStack = new Stack<int>(numbers);
```

**15. Create a Queue from an existing ICollection**

```csharp
Queue<int> numberQueue = new Queue<int>(numbers);
```

**16. Create a LinkedList from an existing ICollection**

```csharp
LinkedList<int> numberLinkedList = new LinkedList<int>(numbers);
```

**17. Iterate over the elements of an ICollection in reverse order**

```csharp
for (int i = numbers.Count - 1; i >= 0; i--)
{
    Console.WriteLine(numbers[i]);
}
```

**18. Remove all elements from an ICollection that match a given predicate**

```csharp
numbers.RemoveAll(number => number % 2 == 0);
```

**19. Find the first element in an ICollection that matches a given predicate**

```csharp
int firstEvenNumber = numbers.Find(number => number % 2 == 0);
```

**20. Find the last element in an ICollection that matches a given predicate**

```csharp
int lastEvenNumber = numbers.FindLast(number => number % 2 == 0);
```

**21. Find the index of the first element in an ICollection that matches a given predicate**

```csharp
int indexOfFirstEvenNumber = numbers.FindIndex(number => number % 2 == 0);
```

**22. Find the index of the last element in an ICollection that matches a given predicate**

```csharp
int indexOfLastEvenNumber = numbers.FindLastIndex(number => number % 2 == 0);
```

**23. Convert an ICollection to a read-only collection**

```csharp
ICollection<int> readOnlyNumbers = numbers.AsReadOnly();
```

**24. Create a synchronized ICollection**

```csharp
ICollection<int> synchronizedNumbers = Collection.Synchronized(numbers);
```

**25. Create an unmodifiable ICollection**

```csharp
ICollection<int> unmodifiableNumbers = Collection.Unmodifiable(numbers);
```

**26. Create a thread-safe ICollection**

```csharp
ICollection<int> threadSafeNumbers = new ConcurrentBag<int>(numbers);
```

**27. Create an observable ICollection**

```csharp
IObservable<ICollection<int>> observableNumbers = Observable.FromEvent<EventHandler<CollectionChangedEventArgs>, ICollection<int>>(
    h => numbers.CollectionChanged += h,
    h => numbers.CollectionChanged -= h);
```

**28. Create an ICollection from an existing IEnumerable**

```csharp
ICollection<int> newNumbers = new List<int>(Enumerable.Range(1, 10));
```

**29. Create an ICollection from an existing IQueryable**

```csharp
ICollection<int> newNumbers = new List<int>(context.Numbers.Where(n => n % 2 == 0));
```

**30. Create an ICollection from an existing IDictionary**

```csharp
ICollection<int> newNumbers = new List<int>(dictionary.Values);
```

**31. Create an ICollection from an existing ISet**

```csharp
ICollection<int> newNumbers = new List<int>(set);
```

**32. Create an ICollection from an existing IReadOnlyCollection**

```csharp
ICollection<int> newNumbers = new List<int>(readOnlyCollection);
```

**33. Create an ICollection from an existing IReadOnlyList**

```csharp
ICollection<int> newNumbers = new List<int>(readOnlyList);
```

**34. Create an ICollection from an existing IReadOnlyDictionary**

```csharp
ICollection<int> newNumbers = new List<int>(readOnlyDictionary.Values);
```

**35. Create an ICollection from an existing IReadOnlySet**

```csharp
ICollection<int> newNumbers = new List<int>(readOnlySet);
```

**36. Create an ICollection from an existing IObservable**

```csharp
ICollection<int> newNumbers = new List<int>();
observableNumbers.Subscribe(n => newNumbers.Add(n));
```

**37. Add a range of elements to an ICollection**

```csharp
numbers.AddRange(new int[] { 4, 5, 6 });
```

**38. Remove a range of elements from an ICollection**

```csharp
numbers.RemoveRange(2, 2);
```

**39. Insert an element into an ICollection at a specified index**

```csharp
numbers.Insert(1, 2);
```

**40. Copy a range of elements from one ICollection to another**

```csharp
numbers.CopyTo(newNumbers, 0, 2);
```

**41. Reverse the order of the elements in an ICollection using LINQ**

```csharp
numbers = numbers.Reverse().ToList();
```

**42. Sort the elements in an ICollection using LINQ**

```csharp
numbers = numbers.OrderBy(n => n).ToList();
```

**43. Find the first element in an ICollection using LINQ**

```csharp
int firstNumber = numbers.First();
```

**44. Find the last element in an ICollection using LINQ**

```csharp
int lastNumber = numbers.Last();
```

**45. Find the index of the first element in an ICollection using LINQ**

```csharp
int indexOfFirstNumber = numbers.IndexOf(1);
```

**46. Find the index of the last element in an ICollection using LINQ**

```csharp
int indexOfLastNumber = numbers.LastIndexOf(1);
```

\*\*47. Create a new ICollection from
