List in C#

Here are some examples of working with lists in C#:

Example 1: Initializing a List<string> with Many String Values

List<string> fruits = new List<string> {
    "apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew", "kiwi"
};

In this example, a List<string> is initialized with several string values using object initializer syntax.

Example 2: Adding an Item to a List

List<int> numbers = new List<int>();

numbers.Add(1);

In this example, an empty List<int> is created and an integer value is added to it using the Add method.

Example 3: Adding an Item to the Beginning of a List

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

numbers.Insert(0, 0);

In this example, a List<int> is initialized with three integer values, and then an integer value is added to the beginning of the list using the Insert method.

Example 4: Adding List<T> to the Beginning of Another List<T>

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

numbers1.InsertRange(0, numbers2);

In this example, two List<int> objects are created, and then the second list is added to the beginning of the first list using the InsertRange method.

Example 5: Converting Comma Separated String into a List<int>

string numbersString = "1,2,3,4,5";
List<int> numbers = numbersString.Split(',').Select(int.Parse).ToList();

In this example, a comma-separated string of integer values is first split into an array of string values using the Split method. The resulting array of string values is then converted to a list of integer values using the Select method and the int.Parse method.

Example 6: Finding Duplicates in List by LINQ

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

var duplicates = numbers.GroupBy(x => x)
                        .Where(g => g.Count() > 1)
                        .Select(g => g.Key);

foreach (var duplicate in duplicates) {
    Console.WriteLine(duplicate);
}

In this example, a List<int> is initialized with several integer values, and then the duplicate values are found using LINQ. The GroupBy method is used to group the elements by their value, and then the Where method is used to filter the groups to include only those with more than one element. The resulting groups are then projected to their key value (i.e., the duplicate value) using the Select method.

Example 7: Finding Unique Values in List by LINQ

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

var unique = numbers.Distinct();

foreach (var number in unique) {
    Console.WriteLine(number);
}

In this example, a List<int> is initialized with several integer values, and then the unique values are found using LINQ. The Distinct method is used to remove duplicates and return a sequence of distinct elements.

Example 8: Counting Each of the Duplicates in List by LINQ

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

var duplicateCounts = numbers.GroupBy(x => x)
                             .Where(g => g.Count() > 1)
                             .Select(g => new { Value = g.Key, Count = g.Count() });

foreach (var duplicate in duplicateCounts) {
    Console.WriteLine($"{duplicate.Value} appears {duplicate.Count} times");
}

In this example, a List<int> is initialized with several integer values, and then the count of each duplicate value is found using LINQ. The GroupBy method is used to group the elements by their value, and then the Where method is used to filter the groups to include only those with more than one element. The resulting groups are then projected to anonymous objects containing the value and count using the Select method.

Example 9: Reversing a List in C#

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

numbers.Reverse();

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

In this example, a List<int> is initialized with several integer values, and then the list is reversed using the Reverse method. The resulting list is then printed to the console. Note that the Reverse method modifies the original list in place, rather than returning a new reversed list.

  1. C# List examples:

    List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
    
  2. Adding elements to a List in C#:

    List<string> fruits = new List<string>();
    fruits.Add("Apple");
    fruits.Add("Banana");
    
  3. Removing items from a List in C#:

    List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
    numbers.Remove(3); // Removes the first occurrence of 3
    
  4. Iterating over a List in C#:

    List<string> colors = new List<string> { "Red", "Green", "Blue" };
    foreach (var color in colors)
    {
        Console.WriteLine(color);
    }
    
  5. Sorting a List in C#:

    List<int> numbers = new List<int> { 5, 2, 8, 1, 9 };
    numbers.Sort(); // Sorts in ascending order
    
  6. Searching for elements in a List in C#:

    List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
    bool containsBanana = fruits.Contains("Banana");
    
  7. Filtering a List in C#:

    List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
    var evenNumbers = numbers.Where(num => num % 2 == 0).ToList();
    
  8. Initializing a List in C#:

    List<double> prices = new List<double>(new double[] { 10.5, 20.0, 15.75 });
    
  9. List of objects in C#:

    List<Person> persons = new List<Person> { /* List of Person objects */ };
    
  10. Copying a List in C#:

    List<int> originalList = new List<int> { 1, 2, 3 };
    List<int> copiedList = new List<int>(originalList);
    
  11. Clearing a List in C#:

    List<string> items = new List<string> { "A", "B", "C" };
    items.Clear();