Lists - potatoscript/csharp GitHub Wiki

πŸ₯” Lists in C# πŸ₯”

πŸ₯” What is a List? πŸ₯”

A List is like an array but with superpowers! It’s a collection that can hold a bunch of items, just like an array, but it can grow and shrink in size automatically. You don’t have to specify the size of the list when you create it, and you can add or remove items anytime!

While an array is like a box with a fixed number of compartments, a List is like a magical box that can expand and shrink as you need it.

πŸ₯” Why Use Lists? πŸ₯”

  • Flexible Size: Unlike arrays, Lists can grow or shrink in size as items are added or removed.
  • Easy to Use: Lists have built-in methods for adding, removing, and searching items, which makes working with them easier than arrays.
  • More Features: Lists offer a lot of powerful methods for sorting, searching, and modifying data.

πŸ₯” Creating a List πŸ₯”

To use a List in C#, you need to import the System.Collections.Generic namespace. This allows you to use the List class and its methods.

Here’s how you can create a List:

// Importing the necessary namespace
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Creating an empty List of integers
        List<int> numbers = new List<int>();

        // Adding items to the List
        numbers.Add(10);
        numbers.Add(20);
        numbers.Add(30);

        // Displaying the items in the List
        Console.WriteLine("List of numbers:");
        foreach (int number in numbers)
        {
            Console.WriteLine(number);  // Output: 10, 20, 30
        }
    }
}

πŸ₯” Adding Items to a List πŸ₯”

You can add items to a List using the .Add() method. The .Add() method places the new item at the end of the list.

// Create a List of strings
List<string> fruits = new List<string>();

// Adding items to the List
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Orange");

// Displaying items in the List
foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);  // Output: Apple, Banana, Orange
}

πŸ₯” Accessing Items in a List πŸ₯”

You can access an item in a List by using its index (just like arrays). Remember, List indices start at 0, so the first item is at index 0, the second at index 1, and so on.

List<string> fruits = new List<string> { "Apple", "Banana", "Orange" };

// Accessing items by index
Console.WriteLine(fruits[0]);  // Output: Apple
Console.WriteLine(fruits[1]);  // Output: Banana
Console.WriteLine(fruits[2]);  // Output: Orange

πŸ₯” Removing Items from a List πŸ₯”

You can remove items from a List using different methods:

  • .Remove() removes the first occurrence of a specific item.
  • .RemoveAt() removes an item at a specific index.
  • .Clear() removes all items from the List.

Here’s how to remove items from a List:

List<string> fruits = new List<string> { "Apple", "Banana", "Orange" };

// Remove a specific item
fruits.Remove("Banana");

// Remove an item at a specific index
fruits.RemoveAt(0);  // Removes "Apple"

// Displaying remaining items in the List
foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);  // Output: Orange
}

// Remove all items from the List
fruits.Clear();

// Displaying the List after clearing
Console.WriteLine("Number of items in the list after clearing: " + fruits.Count);  // Output: 0

πŸ₯” Looping Through a List πŸ₯”

Just like arrays, you can use a foreach loop or a for loop to go through all the items in a List.

πŸ₯” Using a For Loop πŸ₯”

List<string> fruits = new List<string> { "Apple", "Banana", "Orange" };

// Using a for loop
for (int i = 0; i < fruits.Count; i++)
{
    Console.WriteLine(fruits[i]);  // Output: Apple, Banana, Orange
}

πŸ₯” Using a Foreach Loop πŸ₯”

List<string> fruits = new List<string> { "Apple", "Banana", "Orange" };

// Using a foreach loop
foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);  // Output: Apple, Banana, Orange
}

πŸ₯” Other Useful Methods for Lists πŸ₯”

Here are some more useful methods you can use with Lists:

  • .Count: Returns the number of elements in the List.
  • .Contains(): Checks if the List contains a specific item.
  • .IndexOf(): Finds the index of a specific item.
  • .Sort(): Sorts the items in the List.
  • .Reverse(): Reverses the order of the items in the List.
List<int> numbers = new List<int> { 30, 10, 50, 40, 20 };

// Count the number of items in the List
Console.WriteLine("Number of items: " + numbers.Count);  // Output: 5

// Check if the List contains a specific item
Console.WriteLine("Contains 30: " + numbers.Contains(30));  // Output: True

// Find the index of a specific item
Console.WriteLine("Index of 40: " + numbers.IndexOf(40));  // Output: 3

// Sort the List
numbers.Sort();
Console.WriteLine("Sorted List:");
foreach (int number in numbers)
{
    Console.WriteLine(number);  // Output: 10, 20, 30, 40, 50
}

// Reverse the List
numbers.Reverse();
Console.WriteLine("Reversed List:");
foreach (int number in numbers)
{
    Console.WriteLine(number);  // Output: 50, 40, 30, 20, 10
}

πŸ₯” When to Use Lists? πŸ₯”

You should use Lists when:

  • You need a dynamic collection where you can add or remove items freely.
  • You want to easily manage collections of data and have powerful built-in methods to manipulate them.
  • You need random access to items in the collection (via an index) but also want the collection to grow and shrink automatically.

πŸ₯” List vs. Array πŸ₯”

  • Arrays are of fixed size, meaning you have to know the size upfront. They are faster when it comes to memory access but less flexible.
  • Lists are more flexible, allowing you to dynamically change their size and providing more powerful methods for managing the data.

πŸ₯” Summary of Lists in C# πŸ₯”

  • A List is a collection that can store a variable number of items of the same type.
  • Lists grow and shrink automatically, so you don’t need to know the size beforehand.
  • Lists provide many helpful methods, like .Add(), .Remove(), .Sort(), and .Clear(), to make working with collections easy.
  • Lists are often more convenient than arrays when you need to frequently add or remove items.
⚠️ **GitHub.com Fallback** ⚠️