# IList

***

**1. Simple List Initialization and Iteration**

```csharp
IList<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

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

**2. Adding and Removing Elements**

```csharp
IList<string> names = new List<string>();

names.Add("John");
names.Add("Mary");
names.Remove("John");
```

**3. Accessing Elements by Index**

```csharp
IList<char> letters = new List<char> { 'a', 'b', 'c', 'd', 'e' };

char firstLetter = letters[0];
char lastLetter = letters[letters.Count - 1];
```

**4. Inserting Elements at a Specific Index**

```csharp
IList<int> numbers = new List<int> { 1, 2, 3 };

numbers.Insert(1, 4);
```

**5. Searching for Elements**

```csharp
IList<string> names = new List<string> { "John", "Mary", "Bob", "Alice" };

bool isJohnPresent = names.Contains("John");
```

**6. Sorting Elements**

```csharp
IList<int> numbers = new List<int> { 5, 2, 1, 3, 4 };

numbers.Sort();
```

**7. Reversing Elements**

```csharp
IList<string> names = new List<string> { "John", "Mary", "Bob", "Alice" };

names.Reverse();
```

**8. Clearing the List**

```csharp
IList<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

numbers.Clear();
```

**9. Cloning the List**

```csharp
IList<int> numbers1 = new List<int> { 1, 2, 3, 4, 5 };
IList<int> numbers2 = numbers1.ToList();
```

**10. Concatenating Lists**

```csharp
IList<int> numbers1 = new List<int> { 1, 2, 3 };
IList<int> numbers2 = new List<int> { 4, 5, 6 };

IList<int> combinedNumbers = numbers1.Concat(numbers2).ToList();
```

**11. Finding the Index of an Element**

```csharp
IList<string> names = new List<string> { "John", "Mary", "Bob", "Alice" };

int indexOfBob = names.IndexOf("Bob");
```

**12. Finding the Last Index of an Element**

```csharp
IList<int> numbers = new List<int> { 1, 2, 3, 4, 5, 2 };

int lastIndexOf2 = numbers.LastIndexOf(2);
```

**13. Removing Duplicates**

```csharp
IList<int> numbers = new List<int> { 1, 2, 3, 1, 2, 3 };

IList<int> distinctNumbers = numbers.Distinct().ToList();
```

**14. Using Generics**

```csharp
IList<T> list = new List<T>();
list.Add(new T());
```

**15. Using Interfaces**

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

**16. Creating a Read-Only List**

```csharp
IList<int> numbers1 = new List<int> { 1, 2, 3 };
IList<int> numbers2 = new ReadOnlyCollection<int>(numbers1);
```

**17. Iterating with a For Loop**

```csharp
IList<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

for (int i = 0; i < numbers.Count; i++)
{
    Console.WriteLine(numbers[i]);
}
```

**18. Iterating with a Foreach Loop**

```csharp
IList<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

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

**19. Using Linq to Filter Elements**

```csharp
IList<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

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

**20. Using Linq to Project Elements**

```csharp
IList<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

var squaredNumbers = numbers.Select(n => n * n);
```

**21. Using Linq to Group Elements**

```csharp
IList<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };

var groupedNumbers = numbers.GroupBy(n => n % 2 == 0);
```

**22. Using Linq to Aggregate Elements**

```csharp
IList<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

int sum = numbers.Sum();
```

**23. Using a List as a Stack**

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

numbers.Push(1);
numbers.Push(2);
numbers.Push(3);

int lastNumber = numbers.Pop();
```

**24. Using a List as a Queue**

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

numbers.Enqueue(1);
numbers.Enqueue(2);
numbers.Enqueue(3);

int firstNumber = numbers.Dequeue();
```

**25. Using a List as a Deque**

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

numbers.AddToFront(1);
numbers.AddToFront(2);
numbers.AddToFront(3);

int firstNumber = numbers.RemoveFromFront();
```

**26. Implementing a Custom Comparer**

```csharp
class CustomComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        return x.Length.CompareTo(y.Length);
    }
}

IList<string> names = new List<string>();
names.Sort(new CustomComparer());
```

**27. Using a List as a Random Access List**

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

numbers[0] = 1;
int secondNumber = numbers[1];
```

**28. Using a List as a Synchronized List**

```csharp
IList<int> numbers = new List<int>();
lock (numbers)
{
    numbers.Add(1);
}
```

**29. Using a Immutable List**

```csharp
IList<int> numbers = ImmutableList.Create(1, 2, 3);
```

**30. Using a Thread-Safe List**

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

**31. Using a Linked List**

```csharp
IList<string> names = new LinkedList<string>();
names.AddFirst("John");
names.AddLast("Mary");
```

**32. Using a Sorted List**

```csharp
IList<int> numbers = new SortedList<int, string>();
numbers.Add(1, "One");
numbers.Add(2, "Two");
```

**33. Using a Stack**

```csharp
Stack<int> numbers = new Stack<int>();
numbers.Push(1);
numbers.Push(2);
int topNumber = numbers.Pop();
```

**34. Using a Queue**

```csharp
Queue<string> names = new Queue<string>();
names.Enqueue("John");
names.Enqueue("Mary");
string nextName = names.Dequeue();
```

**35. Using a Priority Queue**

```csharp
PriorityQueue<int> numbers = new PriorityQueue<int>();
numbers.Enqueue(1);
numbers.Enqueue(2);
int highestPriorityNumber = numbers.Dequeue();
```

**36. Using a HashSet**

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

**37. Using a Stack as a LIFO (Last-In-First-Out) Data Structure**

```csharp
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
int topElement = stack.Pop();
```

**38. Using a Queue as a FIFO (First-In-First-Out) Data Structure**

```csharp
Queue<int> queue = new Queue<int>();
queue.Enqueue(1);
queue.Enqueue(2);
int nextElement = queue.Dequeue();
```

**39. Using a Dictionary as a Key-Value Store**

```csharp
IDictionary<string, int> ages = new Dictionary<string, int>();
ages["John"] = 25;
ages["Mary"] = 30;
int age = ages["John"];
```

**40. Using a SortedDictionary as a Key-Value Store with Sorted Keys**

```csharp
IDictionary<string, int> ages = new SortedDictionary<string, int>();
ages["John"] = 25;
ages["Mary"] = 30;
KeyValuePair<string, int> firstPair = ages.First();
```

**41. Using a ConcurrentDictionary as a Thread-Safe Key-Value Store**

```csharp
IDictionary<string, int> ages = new ConcurrentDictionary<string, int>();
ages["John"] = 25;
ages["Mary"] = 30;
int age = ages["John"];
```

**42. Using a HashSet as a Set Data Structure**

```csharp
ISet<string> names = new HashSet<string>();
names.Add("John");
names.Add("Mary");
bool containsName = names.Contains("John");
```

**43. Using a SortedSet as a Set Data Structure with Sorted Elements**

```csharp
ISet<string> names = new SortedSet<string>();
names.Add("John");
names.Add("Mary");
string firstElement = names.First();
```

**44. Using a ConcurrentBag as a Thread-Safe Bag Data Structure**

```csharp
IProducerConsumerCollection<string> names = new ConcurrentBag<string>();
names.TryAdd("John");
names.TryAdd("Mary");
string nextName = names.Take();
```

**45. Using a BlockingCollection as a Thread-Safe Blocking Data Structure**

```csharp
BlockingCollection<string> names = new BlockingCollection<string>();
names.Add("John");
names.Add("Mary");
string nextName = names.Take();
```

**46. Using a List as a Data Transfer Object (DTO)**

```csharp
public class PersonDTO
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

IList<PersonDTO> people = new List<PersonDTO>();
people.Add(new PersonDTO { Id = 1, Name = "John", Age = 25 });
```

**47. Using a List as a Storage Mechanism**

```csharp
public class DatabaseService
{
    private static IList<User> _users = new List<User>();

    public static void AddUser(User user)
    {
        _users.Add(user);
    }

    public static IList<User> GetAllUsers()
    {
        return _users;
    }
}
```

**48. Using a List as a Return Type**

```csharp
public IList<string> GetCustomerNames()
{
    IList<string> names = new List<string>();
    names.Add("John");
    names.Add("Mary");
    return names;
}
```

**49. Using a List as a Parameter**

```csharp
public void UpdateCustomerDetails(IList<Customer> customers)
{
    foreach (var customer in customers)
    {
        // Update customer details
    }
}
```

**50. Using a List as a Property**

```csharp
public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IList<Order> Orders { get; set; }
}
```
