C# Collections – Arrays – 1st Part

Hi Friends,

In this section, we are going to start by digging inside Arrays. Arrays one of the most basic yet important piece of C# Collections. One must have profound knowledge of Arrays in order to understand different collection types better. below in the example i have created a simple array for months of year. Any array declaration begins with [] bracket

Now, when i ran the above program in debugging mode, you notice that 1st of all it listed all the elements what i listed there and also in the same order how i have given there. It means its Arrays are ordered list and index based.

1st

Below, in the script, i have added couple of lines to illustrate accessing the element based on index.

2nd

we can write more generalize way of this by giving user flexibility to enter the option like shown below.

Now, when i see the output of this, it will look something like below

3rd

Also, if i have to enumerate through this array, then i will simple write one foreach loop as shown below

4th

Till now, we have seen all readonly operations, now lets see few write operations. Below, snippet will simply go ahead and replace the item in the array. However, replace is equivalent to remove and add element to array, but replace is quite efficient doing the same job.

5th

Now, there are 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 CLR level itself. However, many collections like List under the hood using Arrays for their implementation. So, List is basically dependent on T[].

One more point to note here is Arrays are reference types. so, the very basic test to check this by initializing some value and see the same in debugger, for value types it will be default value for that variable and for reference type it will be null as shown below in the screen shot.

6th

7th

However, since Arrays are reference types, so we can go ahead and create the Array with New keyword as well as shown below in the example.

Now, since i have initialized my array here with int type, so now when i go ahead and the see the same then, it will present me the default values as shown below in the screen shot.

8th

However, in the below example, I have created and initialized the array as well.

Now, when i see the same in debugger, it will present the below values.

9th

One point to note here you can’t initialize array in Array constructor, .Net don’t support that. Only thing which you can do with array constructors is allocate the memory for that.However, C# compiler is very flexible to understand the intention of coder as explained below. here, i have done the array initialization but different ways, all are producing the same result.

next look at enumerating the array, we have already seen implicit style of enumeration using foreach loop, however we can also step through explicitly and the easiest way is using for loop. below i have shown one sample snippet for the same.

One more point to note here is that foreach is readonly operation. means you can’t set values to variables inside foreach loop. With this i would like to conclude the 1st section of the Array. In the next section, we’ll do deep dive under the hood of array.

Thanks,
Rahul
Happy Coding

This entry was posted in C# and tagged . Bookmark the permalink.