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.
Section overview
5 resourcesIntroduces 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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article