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# Data Structures


Share

Data Structures

A data structure is a way to store and organize many values, so they are easy to use.

You already know one data structure: the array. An array stores many values in one variable.

But an array has a fixed size. If you create an array with 3 items, it always has 3 items. You cannot add a fourth item later, and you cannot remove an item.

Often, you don't know how many values you will have. Think of a shopping list: it grows when you add items, and it gets shorter when you buy them.

Arrays are still a good choice when the number of values never changes, like the 7 days of a week.


Collections

C# has ready-made data structures that can grow and shrink. They are called collections.

The collections are in the System.Collections.Generic namespace. To use them, add this line at the top of your file, below using System;:

Example

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

These are the most common collections:

CollectionWhat it storesUse it when
ListValues in order, like an array that can growYou want to add and remove values, and get them by index
DictionaryPairs of keys and valuesYou want to find a value by a key, like a phone number by a name
HashSetValues, each value only onceYou don't want the same value two times
StackValues in a pile: the last value in is the first value outYou want the newest value first, like the Undo button
QueueValues in a line: the first value in is the first value outYou want the oldest value first, like a line at a shop


A First Example

Here is a small example with a List. It starts empty, and you can add as many cars as you want:

Example

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

Try it Yourself »

Example explained

  • List<string> is a list that stores strings. The type inside < > tells C# what kind of values the list holds.
  • Add() adds a value to the end of the list.
  • Count tells you how many values the list has. It changes from 3 to 4 when you add Mazda.

You will learn all about lists in the next chapter, and then about the other collections.


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.