Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele List Operations and Methods | Working with Lists and Arrays
C# Lists & Collections

bookList Operations and Methods

Working with lists in C# becomes much more powerful when you use built-in methods to search, sort, and modify your data. Some of the most commonly used methods include:

  • Contains: checks if a list has a specific value;
  • IndexOf: finds the index of the first occurrence of a value in the list;
  • Sort: arranges the elements of the list in ascending order (alphabetically for strings, numerically for numbers);
  • Reverse: reverses the order of the elements in the list.

These methods help you quickly find, organize, and update your data without writing complex logic from scratch.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> fruits = new List<string> { "Banana", "Apple", "Cherry", "Date" }; // Search for an item in the list string searchItem = "Apple"; bool hasApple = fruits.Contains(searchItem); Console.WriteLine($"List contains '{searchItem}': {hasApple}"); // Sort the list alphabetically fruits.Sort(); Console.WriteLine("Sorted list:"); foreach (string fruit in fruits) { Console.WriteLine(fruit); } } } }

In this example, you start with a list of fruit names. The Contains method checks if "Apple" is present in the list and prints the result. Then, the Sort method arranges the list in alphabetical order. After sorting, you print each fruit to show the new order. Searching and sorting with these methods is efficient and requires only a single line of code for each operation.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> colors = new List<string> { "Red", "Green", "Blue", "Yellow" }; // Reverse the list colors.Reverse(); Console.WriteLine("Reversed list:"); foreach (string color in colors) { Console.WriteLine(color); } // Find the index of "Green" int greenIndex = colors.IndexOf("Green"); Console.WriteLine($"Index of 'Green': {greenIndex}"); } } }

1. Which method checks if a List contains a specific value?

2. What does the Sort() method do to a List?

3. Fill in the blanks to reverse a List<string> named colors.

question mark

Which method checks if a List contains a specific value?

Select the correct answer

question mark

What does the Sort() method do to a List?

Select the correct answer

question-icon

Fill in the blanks to reverse a List<string> named colors.

List colors = new List { "Red", "Green", "Blue" }; colors.();

Click or drag`n`drop items and fill in the blanks

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 4

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookList Operations and Methods

Pyyhkäise näyttääksesi valikon

Working with lists in C# becomes much more powerful when you use built-in methods to search, sort, and modify your data. Some of the most commonly used methods include:

  • Contains: checks if a list has a specific value;
  • IndexOf: finds the index of the first occurrence of a value in the list;
  • Sort: arranges the elements of the list in ascending order (alphabetically for strings, numerically for numbers);
  • Reverse: reverses the order of the elements in the list.

These methods help you quickly find, organize, and update your data without writing complex logic from scratch.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> fruits = new List<string> { "Banana", "Apple", "Cherry", "Date" }; // Search for an item in the list string searchItem = "Apple"; bool hasApple = fruits.Contains(searchItem); Console.WriteLine($"List contains '{searchItem}': {hasApple}"); // Sort the list alphabetically fruits.Sort(); Console.WriteLine("Sorted list:"); foreach (string fruit in fruits) { Console.WriteLine(fruit); } } } }

In this example, you start with a list of fruit names. The Contains method checks if "Apple" is present in the list and prints the result. Then, the Sort method arranges the list in alphabetical order. After sorting, you print each fruit to show the new order. Searching and sorting with these methods is efficient and requires only a single line of code for each operation.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> colors = new List<string> { "Red", "Green", "Blue", "Yellow" }; // Reverse the list colors.Reverse(); Console.WriteLine("Reversed list:"); foreach (string color in colors) { Console.WriteLine(color); } // Find the index of "Green" int greenIndex = colors.IndexOf("Green"); Console.WriteLine($"Index of 'Green': {greenIndex}"); } } }

1. Which method checks if a List contains a specific value?

2. What does the Sort() method do to a List?

3. Fill in the blanks to reverse a List<string> named colors.

question mark

Which method checks if a List contains a specific value?

Select the correct answer

question mark

What does the Sort() method do to a List?

Select the correct answer

question-icon

Fill in the blanks to reverse a List<string> named colors.

List colors = new List { "Red", "Green", "Blue" }; colors.();

Click or drag`n`drop items and fill in the blanks

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 4
some-alt