C# Arrays & Lists

What is an Array?

An array is a collection of elements stored in a fixed-size structure.

All elements must be the same type, and each element is accessed using an index.

Loading...
Output:

Creating Arrays

You can create arrays by specifying the type and size, or by initializing values directly.

Loading...
Output:

Looping Through Arrays

Arrays are often used with loops to access all elements.

Loading...
Output:

What is a List?

A List is a dynamic collection that can grow and shrink in size.

It is part of System.Collections.Generic and is more flexible than arrays.

Loading...
Output:

Adding and Removing Items

Lists allow you to easily add and remove elements.

Loading...
Output:

Key Differences

Arrays and Lists serve similar purposes but behave differently:

  • Array: Fixed size
  • List: Dynamic size
  • Array: Faster
  • List: More flexible

When to Use Each

Use arrays when you know the exact number of elements ahead of time.

Use lists when the number of elements may change.

Practice

Create a list of student names, remove one, and print the remaining names.

Loading...
Output:

Need Help?