Introduction

Welcome to the next unit of this course!

Before we get deeper into C# essentials for interview prep, we'll remind ourselves about some C# features — specifically, C# collections like arrays and strings. These collections allow C# to group multiple elements, such as numbers or characters, under a single entity.

Some of these concepts might already be familiar to you, so you can breeze through the beginning until we get to the meat of the course and the path.

Understanding C# Collections

As our starting point, it's crucial to understand what C# collections are. They help us manage multiple values efficiently and are categorized into Arrays, Dictionaries, and more.

We will focus mainly on arrays and strings in C#.

Arrays in C# are fixed-size collections, meaning once an array is defined, its size cannot be changed directly. However, you can use methods like Array.Resize() to create a new array with a different size while preserving the elements of the original array, or manually copy elements with Array.Copy(). While the size is fixed, the array itself is mutable, allowing you to modify its individual elements after creation.

// Example of an array
int[] numbers = { 1, 2, 3, 4, 5 };

On the other hand, strings in C# are immutable, meaning once a string is created, it cannot be changed. Any operations that seem to modify a string, such as concatenation or replacing characters, actually result in the creation of a new string object in memory.

// Example of a string
string greeting = "Hello";

Let's see mutability/immutability in action for arrays and strings.

using System;

class MainClass {
    public static void Main() {
        // Defining an array and a string
        int[] myArray = {1, 2, 3, 4};
        string myString = "hello";

        // Now let's try to change the first element of both these collections
        myArray[0] = 100; // Works
        
        // Uncommenting the below line will throw an error
        // myString[0] = 'H';  // Uncommenting this will cause a compilation error as strings are immutable
    }
}
Diving Into Arrays

Think about managing a list of tasks or inventory items. Without a collection like an array, it would be hard to keep track of multiple values efficiently. In C#, arrays allow us to store items where each has a specific index, making it easy to access or modify them.

Working with Arrays is as simple as this:

using System;

class MainClass {
    public static void Main() {
        // Creating an array
        string[] fruits = { "apple", "banana", "cherry" };

        // Modifying an element (since it's a fixed-size collection, we can't add items but we can modify existing ones)
        fruits[0] = "apricot";  // ["apricot", "banana", "cherry"]

        // Accessing elements using indexing
        string firstFruit = fruits[0];  // "apricot"
        string lastFruit = fruits[fruits.Length - 1];  // "cherry"

        // Resizing the array
        Array.Resize(ref fruits, 5);  // Increases array size to 5
        // The new elements are initialized to null by default
        fruits[3] = "date";
        fruits[4] = "elderberry";

        // Copying the array
        string[] copiedFruits = new string[5];
        Array.Copy(fruits, copiedFruits, 5);

        // Display the resized array elements
        for (int i = 0; i < fruits.Length; i++)
        {
            if (fruits[i] != null) {
                Console.WriteLine(fruits[i]);  // Outputs each fruit including new elements
            }
        }

        Console.WriteLine("Copied Fruits:");
        for (int i = 0; i < copiedFruits.Length; i++)
        {
            if (copiedFruits[i] != null) {
                Console.WriteLine(copiedFruits[i]);  // Outputs each fruit from the copied array
            }
        }
    }
}

In the example above, Array.Resize(ref fruits, 5) changes the size of the fruits array to 5. The ref keyword is used to pass the array by reference, allowing the method to modify its size. Note that when an array is resized to a larger size, the new elements are initialized to null by default.

Array.Copy(fruits, copiedFruits, 5) is used to copy the contents of the fruits array to the copiedFruits array.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal