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.
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.
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.
