C# Collection – Array 2nd Part

Hi Friends,

Now in this section, we are going to delve inside the arrays. 1st thing which i am going to do here is equality check. so, if you look at the below code you will find, it is comparing two elements and it’s returning for the same value true for 1st case and false for 2nd case. Now, reason for the same is because its comparing the references not the value. and this logic applies not only to arrays rather to all Microsoft Collections.


so, it means all arrays are derived from system.object types and we can prove the same with a small illustration.


One more important point with arrays is you can any time cast array of derived type to array of base type like shown below in the example. This is also known as array covariance.


Arrays also providing copying array to a different array, here we can use either copyTo or Copy method to achieve the same. Below, in the example i have used copyTo method to copy the array to a new array starting at a specific index. also, in the output i have also compared the two arrays just to prove the point that these arrays are pointing to different locations.


However, to achieve the same there is much cleaner an simpler which LINQ extension provides us and that is ToArray() as well. Now, let’s see ordering of Arrays. .Net also provides flexibility to reverse the array and sort the same in some order. Both methods are overloaded and static which means the array which you want to sort or reverse you need to pass the same as a parameter.


However, i can achieve the same using LINQ extension, but if i do

then it won’t reverse the array rather it will create a new object of IEnumerable. However, Linq reverse expects a variable like shown below.


with one small change it will work as expected.


In the above snippet, i have extended the same linq to copy the array to destination. This is one of the efficient ways of doing the same. However, if i need to sort the same then i will use the sort method as shown below in the snippet. This will sort alphabetically.


However, this results true for those collections which are sortable by nature means array of nos, strings etc…how about sorting those which are of type T. In this case to rescue us IComparer comes into picture. so, let’s suppose a scenario i need to sort the months of the year by the length of month. Now, to do the same i need to write one comparer as shown below


let’s consider a scenario where in your input type is very big let’s suppose an array of million items, and you have to find one element out of that. well most efficient way is to examine the different sorting algos and implement the fastest one. However, there is one which we can use easily is BinarySearch algorithm. This is in built. However, there is one catch with binary search, it expects input order in sorter order. so, my implementation will be something like below.

and it will produce the below output.


With this i would like to finish Arrays implementation. In the next section, we’ll see some other collection and its usage, till then stay tuned and Happy Coding.


Posted in C# | Tagged

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.


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


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


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


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.


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.



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.


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.


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.

Happy Coding

Posted in C# | Tagged