Introduction to List in C# WinForms

Store, search, and manage collections of data in WinForms

In WinForms apps, you often need to store data that grows or changes β€” like user entries, search results, or items in memory. Enter List<T>: a dynamic, flexible collection that lets you add, find, remove, and sort items easily.

Whether you’re working with names, numbers, or custom objects, List<T> gives you full control without needing arrays or databases.


🧰 What Is List<T>?

  • A generic collection that holds items of a specific type (T)
  • Can grow or shrink as needed
  • Part of the System.Collections.Generic namespace

πŸ”§ Add this at the top of your form:

using System.Collections.Generic;

πŸ“¦ Declaring and Initializing a List

List<string> names = new List<string>();
List<int> scores = new List<int>();

βœ… You can store strings, numbers, or even custom classes


βž• Adding Items

names.Add("Alice");
names.Add("Ben");
names.Add("Clara");

You can also populate a list at once:

List<string> colors = new List<string> { "Red", "Blue", "Green" };

πŸ” Searching for Items

if (names.Contains("Clara"))
{
    MessageBox.Show("Found Clara!");
}

int index = names.IndexOf("Ben");

βœ… Use .Contains(), .IndexOf(), .Find(), or LINQ for custom search logic


❌ Removing Items

names.Remove("Alice");        // Removes first match
names.RemoveAt(0);            // Removes by index
names.Clear();                // Removes all items

πŸ“‹ Example: Add & Show Names in a ListBox

List<string> students = new List<string>();

private void btnAdd_Click(object sender, EventArgs e)
{
    students.Add(txtName.Text);
    txtName.Clear();
    RefreshList();
}

private void RefreshList()
{
    listBox1.Items.Clear();
    foreach (string student in students)
        listBox1.Items.Add(student);
}

βœ… Keeps your list in sync with the UI
βœ… You can reuse RefreshList() after removing items too


🧠 Why Use List<T> Instead of Arrays?

FeatureList<T>Array
Resizableβœ… Yes❌ Fixed size
Built-in searchβœ… .Contains() etc.❌ Manual loops needed
Add/Removeβœ… Easy❌ Must copy manually

πŸ§ͺ Quick Challenge

🧩 Build a form that:

  • Has a textbox for adding names
  • Stores names in a List<string>
  • Displays the list in a ListBox
  • Lets you remove a selected item with a β€œRemove” button

πŸ“š Summary

OperationCode Example
Create ListList<string> names = new();
Add Itemnames.Add("Alice");
Searchnames.Contains("Ben")
Remove by Valuenames.Remove("Alice")
Remove by Indexnames.RemoveAt(0)
Clear Allnames.Clear();

βœ… Best Practices

  • βœ… Use List<T> for any in-memory collection
  • βœ… Wrap UI updates (like ListBox.Items) in a refresh method
  • βœ… Always check .Contains() before removing items
  • βœ… Use List<T> with classes for richer data (e.g., List<Student>)

πŸŽ“ Want to Go Further?

  • Learn about List<T>.Find() and LINQ for advanced searching
  • Sort your list with .Sort() or custom comparers
  • Use List<T> to back forms, tables, or data-bound controls
  • Serialize a List<T> to a file for storage

πŸ’¬ Want help adapting your WinForms UI to use List<T>?
Show us your form layout β€” We’ll help you wire up a clean, dynamic list logic.