C# / C# Collections and Generics

Working with Arrays and Lists in C#

This tutorial will provide an in-depth guide to working with Arrays and Lists in C#. You will learn how to declare, initialize, and manipulate these data structures.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Introduces collections and generics to handle and manipulate data efficiently.

Working with Arrays and Lists in C

1. Introduction

This tutorial will guide you through the process of working with Arrays and Lists in the C# programming language. The aim is to provide you with a thorough understanding of how to declare, initialize, and manipulate these data structures.

By the end of this tutorial, you will be able to:
- Understand the difference between Arrays and Lists
- Declare and Initialize Arrays and Lists
- Manipulate data stored in Arrays and Lists

Prerequisites:
- Basic knowledge of C# programming language
- A text editor for writing code (Visual Studio, Visual Studio Code, Notepad++, etc.)
- .NET framework installed on your machine

2. Step-by-Step Guide

Arrays

An array is a data structure that stores elements of the same type. In C#, arrays are objects.

Declaring and Initializing Arrays

// declaring an array
int[] arrayName;
// initializing an array
arrayName = new int[5]; // array with 5 integer elements

Accessing Array Elements

Array elements are accessed using their index, starting from 0.

int firstElement = arrayName[0]; // accessing the first element

Lists

A List, unlike an array, is a dynamic data structure, meaning it can grow and shrink in size during runtime.

Declaring and Initializing Lists

// declaring and initializing a List
List<int> listName = new List<int>();

Adding and Removing Elements in Lists

// adding an element
listName.Add(1);
// removing an element
listName.Remove(1);

3. Code Examples

Example 1: Working with Arrays

int[] numbers = new int[3]; // Declare and initialize an array with 3 elements

// Assign values to the array
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;

// Print the second element of the array
Console.WriteLine(numbers[1]); // Outputs: 20

Example 2: Working with Lists

List<int> numbers = new List<int>(); // Declare and initialize a List

// Add values to the list
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);

// Print the second element of the list
Console.WriteLine(numbers[1]); // Outputs: 20

4. Summary

In this tutorial, we've covered the basics of working with Arrays and Lists in C#. You should now be able to declare, initialize, and manipulate data stored in these data structures.

To further your understanding, try out different operations on these data structures like searching elements, sorting, etc. Refer to Microsoft's official documentation for more details on Arrays and Lists in C#.

5. Practice Exercises

Exercise 1

Declare an array of 5 integers, assign values to it, and print the third element.

Solution

int[] numbers = new int[5] {1, 2, 3, 4, 5}; // Declare and initialize an array
Console.WriteLine(numbers[2]); // Outputs: 3

Exercise 2

Declare a list of strings, add three names to it, and remove the second name.

Solution

List<string> names = new List<string>(); // Declare and initialize a List
names.Add("John");
names.Add("Jane");
names.Add("Joe");
names.RemoveAt(1); // Remove the second name

foreach(string name in names)
{
    Console.WriteLine(name); // Outputs: John Joe
}

Keep practicing and experimenting with these data structures to get a solid grasp. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Date Difference Calculator

Calculate days between two dates.

Use tool

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

File Size Checker

Check the size of uploaded files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help