Eric Sjöström Jennerstrand

Litium E-commerce, C#, .NET MAUI, Javascript, React, Azure, Git. .NET, Node and more!
C Sharp

Getting Started with Arrays and Collections in C#

Arrays and collections are fundamental concepts in C# programming language, enabling developers to store and manipulate data efficiently. Arrays are data structures that hold a fixed number of elements of the same type, whereas collections are more flexible and can hold objects of various types and sizes. In this article, we will explore the basics of arrays and collections in C# and provide examples of how to use them in your code.

Understanding Arrays in C

Arrays in C# are objects that can hold multiple values of the same type. They are declared using square brackets and can have a fixed or dynamic size. To create an array, you need to specify the type of the array element and the number of elements it should contain.

// declare and initialize an array of integers
int[] numbers = { 1, 2, 3, 4, 5 };

// declare an array of strings with a length of 3
string[] names = new string[3];
names[0] = "John";
names[1] = "Mary";
names[2] = "James";

You can access individual elements of an array using an index, which starts at 0. Arrays in C# are zero-indexed, meaning that the first element has an index of 0, the second element has an index of 1, and so on.

// access the first element of the numbers array
int firstNumber = numbers[0];

// change the third element of the names array
names[2] = "Peter";

Exploring Collections in C

Collections in C# are containers that can hold objects of various types and sizes. Unlike arrays, collections can grow or shrink dynamically, making them more flexible and easier to use. C# provides several built-in collection classes, such as List, Dictionary, Queue, and Stack.

// create a list of strings
List fruits = new List();
fruits.Add("apple");
fruits.Add("banana");
fruits.Add("cherry");

// create a dictionary of integers and strings
Dictionary colors = new Dictionary();
colors.Add(1, "red");
colors.Add(2, "green");
colors.Add(3, "blue");

To access elements of a collection, you can use methods like indexing, iteration, or LINQ queries. For example, to get the first element of a list, you can use the index operator, like this:

string firstFruit = fruits[0];

Or, you can use a foreach loop to iterate over all the elements of a collection:

foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}

Overall, collections are essential tools for managing data in C# programs. They provide a flexible and efficient way to store and manipulate objects, making it easier to write robust and scalable code.

In this article, we have covered the basics of arrays and collections in C#. We have seen how to declare, initialize, and access elements of arrays, as well as how to create and manipulate collections. By mastering these concepts, you can write more efficient and powerful programs that can handle complex data structures. As you continue to work with C#, you will discover even more ways to use arrays and collections to your advantage.

One thought on “Getting Started with Arrays and Collections in C#

  • you know
    2023-10-18 at 20:45

    Nice

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>