Get Items from List in C#

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

Example 1: Getting Items from a List by Index

List<string> names = new List<string> { "Alice", "Bob", "Charlie", "Dave" };

string secondName = names[1];

Console.WriteLine(secondName); // Output: "Bob"

In this example, a List<string> is initialized with several string values, and then the second item in the list is accessed using the square bracket notation and index value 1.

Example 2: Getting Items in One List that are Not in Another List

List<string> list1 = new List<string> { "Alice", "Bob", "Charlie", "Dave" };
List<string> list2 = new List<string> { "Bob", "Eve" };

var itemsNotInList2 = list1.Except(list2);

foreach (var item in itemsNotInList2) {
    Console.WriteLine(item);
}

In this example, two List<string> objects are created, and then the items in list1 that are not in list2 are found using the Except method. The resulting items are then printed to the console.

Example 3: Getting Items in One List that are Also in Another List

List<string> list1 = new List<string> { "Alice", "Bob", "Charlie", "Dave" };
List<string> list2 = new List<string> { "Bob", "Eve" };

var commonItems = list1.Intersect(list2);

foreach (var item in commonItems) {
    Console.WriteLine(item);
}

In this example, two List<string> objects are created, and then the items in list1 that are also in list2 are found using the Intersect method. The resulting items are then printed to the console.

  1. C# get items from List:

    List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
    int specificNumber = numbers[2]; // Retrieves the item at index 2
    
  2. Retrieve elements from List in C#:

    List<string> colors = new List<string> { "Red", "Green", "Blue" };
    string firstColor = colors[0]; // Retrieves the first item in the list
    
  3. Accessing List elements by index in C#:

    List<double> prices = new List<double> { 10.5, 20.0, 15.75 };
    double secondPrice = prices[1]; // Retrieves the second item in the list
    
  4. Getting items from List using LINQ in C#:

    List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
    int specificNumber = numbers.FirstOrDefault(n => n == 3); // Retrieves the first item matching the condition
    
  5. Retrieve multiple items from List in C#:

    List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
    List<int> selectedNumbers = numbers.Where(n => n % 2 == 0).ToList(); // Retrieves items based on a condition
    
  6. Accessing List elements with foreach loop in C#:

    List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
    foreach (var fruit in fruits)
    {
        Console.WriteLine(fruit); // Access elements using foreach loop
    }
    
  7. C# List element retrieval by value:

    List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
    string banana = fruits.Find(fruit => fruit == "Banana"); // Retrieves item by value
    
  8. Finding items in List with Where in C#:

    List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
    List<int> selectedNumbers = numbers.Where(n => n > 2).ToList(); // Retrieves items based on a condition
    
  9. C# List element access by index:

    List<string> colors = new List<string> { "Red", "Green", "Blue" };
    string secondColor = colors.ElementAt(1); // Retrieves the item at index 1
    
  10. Getting the first item from List in C#:

    List<double> prices = new List<double> { 10.5, 20.0, 15.75 };
    double firstPrice = prices.First(); // Retrieves the first item in the list
    
  11. C# List element access by LINQ query:

    List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
    string specificFruit = fruits.First(fruit => fruit.StartsWith("B")); // Retrieves the first item matching the condition
    
  12. Retrieving specific items from List in C#:

    List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
    List<int> selectedNumbers = numbers.FindAll(n => n % 2 == 0); // Retrieves items based on a condition
    
  13. C# List element access by condition:

    List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
    string specificFruit = fruits.Find(fruit => fruit.Length > 5); // Retrieves the first item matching the condition
    
  14. Getting last item from List in C#:

    List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
    int lastNumber = numbers.Last(); // Retrieves the last item in the list
    
  15. C# List element access with Find method:

    List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
    string specificFruit = fruits.Find(fruit => fruit.Contains("a")); // Retrieves the first item matching the condition
    
  16. Retrieving a range of items from List in C#:

    List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
    List<int> selectedNumbers = numbers.GetRange(1, 3); // Retrieves a range of items starting from index 1
    
  17. C# List element access by predicate:

    List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
    string specificFruit = fruits.Find(fruit => fruit.StartsWith("B")); // Retrieves the first item matching the condition