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.


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