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"
- };
- }
- }
- }

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"
- };
- //accessing the element based on lookup
- //and then printing the value
- 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]);
- }
- }
- }

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"
- };
- //Assigning diff value to existing one
- 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.
- //Creating an array of 5 elements
- int[] c = new int[5];

However, in the following example, I have created and initialized the array as well.
- //create and initialize the array
- int[] d = new int[5] { 1, 2, 3, 4, 5 };

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.
- //Creating an array of 5 elements
- int[] c = new int[5];
- //create and initialize the array
- int[] d = new int[5] { 1, 2, 3, 4, 5 };
- //removed the redundant array size
- int[] e = new int[] { 1, 2, 3, 4, 5 };
- //used implicit typing using var
- var f = new int[] { 1, 2, 3, 4, 5 };
- //used explicit type on the left
- int[] g = { 1, 2, 3, 4, 5 };
- //explicit looping
- 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.

Abhishek JaiswalPosted Jun 6, 2015, 1:49 PM
Nice Read!! :)
Santhakumar MunuswamyPosted Jun 6, 2015, 2:19 AM
Thanks for well explained
Pankaj Kumar ChoudharyPosted Jun 5, 2015, 12:26 PM
Nice Article Sir........