# Collections\_Generic\_IList\_T

***

**1. Create and Initialize a List**

```csharp
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
```

**2. Access Elements in a List**

```csharp
int firstNumber = numbers[0]; // 1
int secondNumber = numbers[1]; // 2
```

**3. Iterate Over a List**

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

**4. Insert an Element into a List**

```csharp
numbers.Insert(1, 4); // Insert 4 at index 1
```

**5. Remove an Element from a List**

```csharp
numbers.Remove(2); // Remove the number 2
```

**6. Find the Index of an Element**

```csharp
int indexOfThree = numbers.IndexOf(3); // 2
```

**7. Check if an Element Exists in a List**

```csharp
bool containsFive = numbers.Contains(5); // False
```

**8. Clear a List**

```csharp
numbers.Clear(); // Remove all elements from the list
```

**9. Sort a List**

```csharp
numbers.Sort(); // Sort the list in ascending order
```

**10. Reverse a List**

```csharp
numbers.Reverse(); // Reverse the order of elements in the list
```

**11. Find the Maximum and Minimum Elements**

```csharp
int max = numbers.Max(); // Find the maximum element
int min = numbers.Min(); // Find the minimum element
```

**12. Convert a List to an Array**

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

**13. Convert an Array to a List**

```csharp
List<int> newList = new List<int>(array);
```

**14. Copy a List**

```csharp
List<int> copy = new List<int>(numbers); // Copy the elements of numbers to copy
```

**15. Concatenate Two Lists**

```csharp
List<int> list1 = new List<int> { 1, 2, 3 };
List<int> list2 = new List<int> { 4, 5, 6 };
list1.AddRange(list2); // Append the elements of list2 to list1
```

**16. Remove Duplicate Elements**

```csharp
list1.Distinct(); // Remove duplicate elements from list1
```

**17. Find the Intersection of Two Lists**

```csharp
List<int> intersection = list1.Intersect(list2).ToList();
```

**18. Find the Union of Two Lists**

```csharp
List<int> union = list1.Union(list2).ToList();
```

**19. Find the Difference Between Two Lists**

```csharp
List<int> difference = list1.Except(list2).ToList();
```

**20. Trim Excess Capacity**

```csharp
list1.TrimExcess(); // Reduce the capacity of the list to the number of elements it contains
```

**21. Use List as a Queue**

```csharp
list1.Enqueue(7); // Add an element to the end of the list
int firstElement = list1.Dequeue(); // Remove the first element from the list
```

**22. Use List as a Stack**

```csharp
list1.Push(8); // Add an element to the beginning of the list
int lastElement = list1.Pop(); // Remove the first element from the list
```

**23. Use List as a Read-Only Collection**

```csharp
IReadOnlyList<int> readOnlyList = list1.AsReadOnly(); // Create a read-only view of list1
```

**24. Iterate Over a List in Parallel**

```csharp
Parallel.ForEach(list1, element => Console.WriteLine(element));
```

**25. Use LINQ to Query a List**

```csharp
var evenNumbers = list1.Where(n => n % 2 == 0);
```

**26. Use Lambda Expression to Remove Elements from a List**

```csharp
list1.RemoveAll(n => n > 5);
```

**27. Use List as a Key-Value Store**

```csharp
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary["key1"] = 10;
int value = dictionary["key1"];
```

**28. Use List as a Set**

```csharp
HashSet<string> set = new HashSet<string>();
set.Add("value1");
bool contains = set.Contains("value1");
```

**29. Use List as a Data Structure for a Queue of Strings**

```csharp
Queue<string> queue = new Queue<string>();
queue.Enqueue("value1");
string dequeued = queue.Dequeue();
```

**30. Use List as a Data Structure for a Stack of Integers**

```csharp
Stack<int> stack = new Stack<int>();
stack.Push(10);
int popped = stack.Pop();
```

**31. Use List to Iterate Over a Range of Numbers**

```csharp
foreach (var number in Enumerable.Range(1, 10))
{
    Console.WriteLine(number);
}
```

**32. Use List to Create a Read-Only Collection**

```csharp
IReadOnlyCollection<int> readOnlyCollection = list1.AsReadOnly();
foreach (var number in readOnlyCollection)
{
    Console.WriteLine(number);
}
```

**33. Use List to Create a Concurrent Collection**

```csharp
ConcurrentBag<int> concurrentBag = new ConcurrentBag<int>();
concurrentBag.Add(10);
foreach (var number in concurrentBag)
{
    Console.WriteLine(number);
}
```

**34. Use List to Create a Thread-Safe Collection**

```csharp
ThreadSafeList<int> threadSafeList = new ThreadSafeList<int>();
threadSafeList.Add(10);
foreach (var number in threadSafeList)
{
    Console.WriteLine(number);
}
```

**35. Use List to Create a Case-Insensitive Collection**

```csharp
List<string> caseInsensitiveList = new List<string>(StringComparer.InvariantCultureIgnoreCase);
caseInsensitiveList.Add("value1");
bool contains = caseInsensitiveList.Contains("VALUE1");
```

**36. Use List to Create a Sorted Collection**

```csharp
SortedList<string, int> sortedList = new SortedList<string, int>();
sortedList.Add("key1", 10);
foreach (var keyValuePair in sortedList)
{
    Console.WriteLine(keyValuePair.Key + " : " + keyValuePair.Value);
}
```

**37. Use List to Create a Sorted Set**

```csharp
SortedSet<string> sortedSet = new SortedSet<string>();
sortedSet.Add("value1");
foreach (var value in sortedSet)
{
    Console.WriteLine(value);
}
```

**38. Use List to Create a Keyed Collection**

```csharp
KeyedCollection<string, int> keyedCollection = new KeyedCollection<string, int>();
keyedCollection.Add("key1", 10);
foreach (var keyValuePair in keyedCollection)
{
    Console.WriteLine(keyValuePair.Key + " : " + keyValuePair.Value);
}
```

**39. Use List to Create a Custom Collection**

```csharp
public class CustomCollection<T> : List<T>
{
    public CustomCollection()
    {
        // Custom initialization logic
    }
}
```

**40. Use List to Create a Generic Collection**

```csharp
public class GenericList<T> : List<T>
{
    public GenericList()
    {
        // Custom initialization logic
    }
}
```

**41. Use List to Create a Collection with Custom Comparer**

```csharp
public class CustomComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        // Custom comparison logic
    }
}
```

**42. Use List to Create a Collection with Custom Equality Comparer**

```csharp
public class CustomEqualityComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        // Custom equality comparison logic
    }

    public int GetHashCode(string obj)
    {
        // Custom hash code calculation logic
    }
}
```

**43. Use List to Create a Collection with Custom Hasher**

```csharp
public class CustomHasher : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        // Custom equality comparison logic
    }

    public int GetHashCode(string obj)
    {
        // Custom hash code calculation logic
    }
}
```

**44. Use List to Create a Collection with Custom Serialization**

```csharp
[Serializable]
public class CustomSerializableList<T> : List<T>
{
    public CustomSerializableList()
    {
        // Custom serialization logic
    }
}
```

**45. Use List to Create a Collection with Custom Events**

```csharp
public class CustomEventList<T> : List<T>
{
    public event EventHandler<ListChangedEventArgs<T>> ListChanged;

    public void Add(T item)
    {
        base.Add(item);
        OnListChanged(new ListChangedEventArgs<T>(ListChangedType.ItemAdded, item, index: -1));
    }

    protected virtual void OnListChanged(ListChangedEventArgs<T> e)
    {
        ListChanged?.Invoke(this, e);
    }
}
```

**46. Use List to Create a Collection with Custom Properties**

```csharp
public class CustomPropertyList<T> : List<T>
{
    public int CountOfEvenNumbers
    {
        get { return this.Count(n => n % 2 == 0); }
    }
}
```

**47. Use List to Create a Collection with Custom Methods**

```csharp
public class CustomMethodList<T> : List<T>
{
    public void Shuffle()
    {
        Random rng = new Random();
        int n = this.Count;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            T value = this[k];
            this[k] = this[n];
            this[n] = value;
        }
    }
}
```

**48. Use List to Create a Collection with Custom Indexer**

```csharp
public class CustomIndexerList<T> : List<T>
{
    public T this[string index]
    {
        get { return this[int.Parse(index)]; }
        set { this[int.Parse(index)] = value; }
    }
}
```

**49. Use List to Create a Collection with Extension Methods**

```csharp
public static class ListExtensions
{
    public static void RemoveAll<T>(this List<T> list, Predicate<T> match)
    {
        for (int i = list.Count - 1; i >= 0; i--)
        {
            if (match(list[i]))
            {
                list.RemoveAt(i);
            }
        }
    }
}
```

**50. Use List to Create a Collection with Operator Overloads**

```csharp
public class CustomOperatorList<T> : List<T>
{
    public static CustomOperatorList<T> operator +(CustomOperatorList<T> list1, CustomOperatorList<T> list2)
    {
        CustomOperatorList<T> newList = new CustomOperatorList<T>();
        newList.AddRange(list1);
        newList.AddRange(list2);
        return newList;
    }
}
```
