In this section, we will start by digging inside Arrays. Arrays are one of the most basic yet important spiece of C# Collections. One must have profound knowledge of arrays to understand various collection types better. In the following example, I have created a simple array for months of year. Any array declaration begins with [] bracket.
- namespace Collections
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] monthsofYear =
- {
- "Jan",
- "Feb",
- "Mar",
- "Apr",
- "May",
- "Jun",
- "Jul",
- "Aug",
- "Sep",
- "Oct",
- "Nov",
- "Dec"
- };
- }
- }
- }
Now, when you run the preceding program in debugging mode, you will see that first it listed all the elements what I listed there and also in the same orde. In other words the arrays are an ordered list and are index based.
In the following script, I have added a couple of lines to illustrate the accessing of elements based on index.
- using System;
-
- namespace Collections
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] monthsofYear =
- {
- "Jan",
- "Feb",
- "Mar",
- "Apr",
- "May",
- "Jun",
- "Jul",
- "Aug",
- "Sep",
- "Oct",
- "Nov",
- "Dec"
- };
-
-
- string march = monthsofYear[2];
- Console.WriteLine(march);
- }
- }
- }
We can write a more generalized way to provide the user flexibility to enter the option as in the following code:
- using System;
-
- namespace Collections
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] monthsofYear =
- {
- "Jan",
- "Feb",
- "Mar",
- "Apr",
- "May",
- "Jun",
- "Jul",
- "Aug",
- "Sep",
- "Oct",
- "Nov",
- "Dec"
- };
- Console.WriteLine( "Provide Index position to print the month!");
- int month = int.Parse(Console.ReadLine());
- Console.WriteLine(monthsofYear[month]);
- }
- }
- }
Now, when I see the output of this, it will look something like the following:
Also, if I need to enumerate through this array, then I will simply write one foreach loop as in the following code:
- using System;
-
- namespace Collections
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] monthsofYear =
- {
- "Jan",
- "Feb",
- "Mar",
- "Apr",
- "May",
- "Jun",
- "Jul",
- "Aug",
- "Sep",
- "Oct",
- "Nov",
- "Dec"
- };
- foreach (var month in monthsofYear)
- {
- Console.WriteLine(month);
- }
- Console.ReadLine();
- }
- }
- }
Until now, we have seen all readonly operations, now let's see a few write operations. The following snippet will simply go ahead and replace the items in the array. However, replace is equivalent to remove and add element to the array, but replace is quite efficient in doing the same job.
- using System;
-
- namespace Collections
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] monthsofYear =
- {
- "Jan",
- "Feb",
- "Mar",
- "Apr",
- "May",
- "Jun",
- "Jul",
- "Aug",
- "Sep",
- "Oct",
- "Nov",
- "Dec"
- };
-
-
- monthsofYear[2] = "Financial Month";
- foreach (var month in monthsofYear)
- {
- Console.WriteLine(month);
- }
- Console.ReadLine();
- }
- }
- }
Now, there are a few important points about arrays, like they have their own syntax in C#, you don't declare any other type using [] like we do with generics. Also, their strong typing is implemented at the CLR level itself. However, many collections like List<T> under the hood use arrays for their implementation. So, List<T> is basically dependent on T[].
One more point to note here is that arrays are reference types. So, the basic test to check this is by initializing some value and see it in the debugger, for value types it will be the default value for that variable and for a reference type it will be null as in the following screenshot:
However, since arrays are reference types, we can go ahead and create the array with new keyword as shown in the following example.
Now, since I have initialized my array here with the int type, when I look at it, it will present me the default values as shown in the following screenshot:
However, in the following example, I have created and initialized the array as well.
-
- int[] d = new int[5] { 1, 2, 3, 4, 5 };
Now, when I see it in the debugger, it will present the following values.
One point to note here is that you can't initialize an array in the array constructor, .NET doesn't support that. The only thing you can do with array constructors is allocate the memory for it. However, the C# compiler is very flexible to understand the intent of the coder as shown in the following code snippet. Here, I have done an array initialization in various ways that are producing the same result.
-
- int[] c = new int[5];
-
- int[] d = new int[5] { 1, 2, 3, 4, 5 };
-
- int[] e = new int[] { 1, 2, 3, 4, 5 };
-
- var f = new int[] { 1, 2, 3, 4, 5 };
-
- int[] g = { 1, 2, 3, 4, 5 };
Next look at enumerating the array, we have already seen the implicit style of enumeration using a foreach loop. However we can also step through explicitly and the easiest way is using a for loop. I have shown one sample snippet for that in the following code.
-
- for (int i = 0; i < monthsofYear.Length; i++)
- {
- Console.WriteLine( monthsofYear[i]);
- }
One more point to note here is that foreach is a readonly operation. In other words you can't set values to variables inside a foreach loop.
With this I would like to conclude the first section about arrays. In the next section, we'll do deep dive under the hood of arrays.