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.

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.

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

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:

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.

Understanding Strings

Think of strings as sequences of letters or characters. So, whether you're writing down a message or noting a paragraph, it all boils down to a string in C#. Strings are enclosed by double quotes.

  • ToLower(): Converts all characters in the string to lowercase.

    • Example: "Hello, WORLD!".ToLower() results in "hello, world!".
  • ToUpper(): Converts all characters in the string to uppercase.

    • Example: "Hello, world!".ToUpper() results in "HELLO, WORLD!".
  • Trim(): Removes all leading and trailing whitespace from the string.

    • Example: " Hello, world! ".Trim() results in "Hello, world!".

Though strings are immutable, we can use these methods to effectively work with them. These methods create a new string for us.

Indexing and Common Operations

Both arrays and strings allow us to access individual elements through indexing. In C#, we start counting from 0, implying the first element is at index 0, the second at index 1, and so on. Negative indexing is not directly supported in C#.

We have many operations we can perform on our arrays and strings. We can slice them, concatenate them, repeat them, and even find the occurrence of a particular element!

And there we have it, a C# toolkit of arrays and strings!

Lesson Summary and Practice

Give yourself a pat on the back! You've navigated through C# collections, primarily focusing on arrays and strings, and learning how to create, access, and manipulate them via various operations.

Up next, reinforce your understanding with plenty of hands-on practice. The comprehension of these concepts, combined with frequent practice, will enable you to tackle more complex problem statements with ease. Happy coding!

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