Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# List


Share

List

An array has a fixed size. When you create it, you decide how many items it has, and you cannot add more items later.

A List is like an array that can grow and shrink. Think of a shopping list: you add items when you need them, and you remove items when you are done.

To use a List, add this line at the top of your file:

Example

// Include the namespace that holds List
using System.Collections.Generic;

Create a List

To create a list, write List, the type of the items inside < >, and the name of the list. new List<string>() creates a new, empty list of strings.

Add() adds an item to the end of the list:

Example

List<string> cars = new List<string>();
cars.Add("Volvo");
cars.Add("BMW");
cars.Add("Ford");
cars.Add("Mazda");

foreach (string car in cars)
{
  Console.WriteLine(car);
}

Try it Yourself »

The foreach loop prints all the items, just like with an array.

You can also add the items when you create the list. Write them inside curly braces { }.

To find out how many items a list has, use Count (an array uses Length, but a list uses Count):

Example

List<string> cars = new List<string> { "Volvo", "BMW", "Ford", "Mazda" };
Console.WriteLine(cars.Count);  // 4

Try it Yourself »


Access and Change Items

Like in an array, each item in a list has an index. The first item has index 0, the second has index 1, and so on.

Write the index inside square brackets [ ] to get an item:

Example

List<string> cars = new List<string> { "Volvo", "BMW", "Ford", "Mazda" };
Console.WriteLine(cars[0]);  // Volvo
Console.WriteLine(cars[2]);  // Ford

Try it Yourself »

To change an item, refer to its index:

Example

List<string> cars = new List<string> { "Volvo", "BMW", "Ford", "Mazda" };
cars[0] = "Opel";
Console.WriteLine(cars[0]);  // Opel

Try it Yourself »



Insert and Remove Items

Add() always adds an item to the end. To put an item at another place, use Insert(). The first value is the index, and the second value is the item:

Example

List<string> cars = new List<string> { "Volvo", "BMW", "Ford" };
cars.Insert(1, "Mazda");

foreach (string car in cars)
{
  Console.WriteLine(car);
}

Try it Yourself »

Mazda is now at index 1. The items after it move one place back.

To remove an item, use Remove() with the item, or RemoveAt() with the index:

Example

List<string> cars = new List<string> { "Volvo", "BMW", "Ford", "Mazda" };
cars.Remove("BMW");
cars.RemoveAt(0);

foreach (string car in cars)
{
  Console.WriteLine(car);
}

Try it Yourself »

Remove("BMW") removes BMW. Then RemoveAt(0) removes the first item, Volvo. Only Ford and Mazda are left.


Check If an Item Exists

Contains() returns true if the list has the item, and false if it does not:

Example

List<string> cars = new List<string> { "Volvo", "BMW", "Ford", "Mazda" };
Console.WriteLine(cars.Contains("BMW"));    // True
Console.WriteLine(cars.Contains("Tesla"));  // False

Try it Yourself »


Loop Through a List

You have already used foreach to loop through a list. You can also use a for loop. Use Count to know when to stop:

Example

List<string> cars = new List<string> { "Volvo", "BMW", "Ford", "Mazda" };

for (int i = 0; i < cars.Count; i++)
{
  Console.WriteLine(i + ": " + cars[i]);
}

Try it Yourself »

Use foreach when you only need the items. Use for when you also need the index.


Sort a List

Sort() sorts the items in the list. Strings are sorted alphabetically, and numbers from the smallest to the largest.

This list stores numbers, so its type is List<int>:

Example

List<int> scores = new List<int> { 50, 12, 87, 34 };
scores.Sort();

foreach (int score in scores)
{
  Console.WriteLine(score);
}

Try it Yourself »



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.