Collections and Arrays in C#: Part 2

Now in this section, we will delve into arrays. The first thing to do here is equality check. So, if you look at the following code you will find it is comparing two elements and it's returning for the same value true for the first case and false for the second case. Now the reason for that is because it's comparing the references, not the value. And this logic applies not only to arrays but also to all Microsoft collections.

  1. using System;    
  2.     
  3. namespace Collections    
  4. {    
  5.     class Array    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             int[] a ={ 1, 2, 3, 4, 5 };    
  10.             //copied the array to b    
  11.             var b =a;    
  12.     
  13.             int[] c = {1,2,3,4,5 };    
  14.             Console.WriteLine(  string.Format("a==b is {0}",a==b));    
  15.             Console.WriteLine(  string.Format("b==c is {0}",b==c));    
  16.             Console.ReadLine();    
  17.         }    
  18.             
  19.     }    
  20. }   
important point
So, in other words all arrays are derived from system.object types and we can prove that with a small illustration.
  1. using System;    
  2.     
  3. namespace Collections    
  4. {    
  5.     class Array    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             object[] obj = new object[3]{    
  10.                 "Hello Rahul",    
  11.                 27,    
  12.                 new int[2]{1,2}    
  13.             };    
  14.     
  15.             Type objType = obj.GetType();    
  16.             foreach(object item in obj){    
  17.                 Console.WriteLine(item);    
  18.             }    
  19.         }    
  20.    }    
  21. }    
ARRAY

One more important point with arrays is, you can anytime cast an array of derived types to an array of base types like shown below in the example. This is also known as array covariance.
  1. using System;    
  2.     
  3. namespace Collections    
  4. {    
  5.     class Array    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             string[] monthsofYear =    
  10.             {    
  11.                 "Jan",    
  12.                 "Feb",    
  13.                 "Mar",    
  14.                 "Apr",    
  15.                 "May",    
  16.                 "Jun",    
  17.                 "Jul",    
  18.                 "Aug",    
  19.                 "Sep",    
  20.                 "Oct",    
  21.                 "Nov",    
  22.                 "Dec"    
  23.             };    
  24.     
  25.             //Implicit casting    
  26.             object[] obj = monthsofYear;    
  27.     
  28.                foreach(object item in obj){    
  29.                 Console.WriteLine(item);    
  30.             }    
  31.                Console.ReadLine();    
  32.         }    
  33.             
  34.     }    
  35. }   
achieve

Arrays also provide the ability to copy an array to another array. Here we can use either copyTo or Copy method to do the same. Below, in the example I have used the 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.
  1. using System;    
  2.     
  3. namespace Collections    
  4. {    
  5.     class Array    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             string[] monthsofYear =    
  10.             {    
  11.                 "Jan",    
  12.                 "Feb",    
  13.                 "Mar",    
  14.                 "Apr",    
  15.                 "May",    
  16.                 "Jun",    
  17.                 "Jul",    
  18.                 "Aug",    
  19.                 "Sep",    
  20.                 "Oct",    
  21.                 "Nov",    
  22.                 "Dec"    
  23.             };    
  24.     
  25.             string[] copyArr = new string[16];    
  26.             monthsofYear.CopyTo(copyArr, 4);    
  27.                 
  28.                foreach(string item in copyArr){    
  29.                 Console.WriteLine(item);    
  30.                    
  31.             }    
  32.                Console.WriteLine(string.Format("monthsofyear==copyArr : {0}", monthsofYear == copyArr));    
  33.                Console.ReadLine();    
  34.         }    
  35.             
  36.     }    
  37. }  
Console

However, to do the same thing there is a much cleaner and simpler way that LINQ extensions provide us and that is ToArray() as well. Now, let's see the ordering of Arrays. .Net also provides the ability to reverse the array and sort it in some order. Both methods are overloaded and static, that means you need to pass the array that you want to sort or reverse as a parameter.
  1. using System.Linq;    
  2. using System;    
  3.     
  4. namespace Collections    
  5. {    
  6.     class Array    
  7.     {    
  8.         static void Main(string[] args)    
  9.         {    
  10.             string[] monthsofYear =    
  11.             {    
  12.                 "Jan",    
  13.                 "Feb",    
  14.                 "Mar",    
  15.                 "Apr",    
  16.                 "May",    
  17.                 "Jun",    
  18.                 "Jul",    
  19.                 "Aug",    
  20.                 "Sep",    
  21.                 "Oct",    
  22.                 "Nov",    
  23.                 "Dec"    
  24.             };    
  25.     
  26.             System.Array.Reverse(monthsofYear);    
  27.             foreach (string month in monthsofYear)    
  28.             {    
  29.                 Console.WriteLine(month);    
  30.             }    
  31.             Console.ReadLine();    
  32.         }    
  33.             
  34.     }    
  35. }  
reverse

However, I can do the same using LINQ extension, but if I do:
  1. using System.Linq;    
  2. using System;    
  3.     
  4. namespace Collections    
  5. {    
  6.     class Array    
  7.     {    
  8.         static void Main(string[] args)    
  9.         {    
  10.             string[] monthsofYear =    
  11.             {    
  12.                 "Jan",    
  13.                 "Feb",    
  14.                 "Mar",    
  15.                 "Apr",    
  16.                 "May",    
  17.                 "Jun",    
  18.                 "Jul",    
  19.                 "Aug",    
  20.                 "Sep",    
  21.                 "Oct",    
  22.                 "Nov",    
  23.                 "Dec"    
  24.             };    
  25.             monthsofYear.Reverse();    
  26.             //System.Array.Reverse(monthsofYear);    
  27.             foreach (string month in monthsofYear)    
  28.             {    
  29.                 Console.WriteLine(month);    
  30.             }    
  31.             Console.ReadLine();    
  32.         }    
  33.             
  34.     }    
  35. }   
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.

IEnumerable

With one small change it will work as expected.
  1. using System.Linq;    
  2. using System;    
  3.     
  4. namespace Collections    
  5. {    
  6.     class Array    
  7.     {    
  8.         static void Main(string[] args)    
  9.         {    
  10.             string[] monthsofYear =    
  11.             {    
  12.                 "Jan",    
  13.                 "Feb",    
  14.                 "Mar",    
  15.                 "Apr",    
  16.                 "May",    
  17.                 "Jun",    
  18.                 "Jul",    
  19.                 "Aug",    
  20.                 "Sep",    
  21.                 "Oct",    
  22.                 "Nov",    
  23.                 "Dec"    
  24.             };    
  25.             var reversed=monthsofYear.Reverse();    
  26.             //System.Array.Reverse(monthsofYear);    
  27.             foreach (string month in reversed)    
  28.             {    
  29.                 Console.WriteLine(month);    
  30.             }    
  31.             Console.ReadLine();    
  32.         }    
  33.             
  34.     }    
  35. }  
extension
  1. //Linq extension to copy the collection as array   
  2. var reversed=monthsofYear.Reverse().ToArray();   
In the preceding snippet, I have extended the same LINQ to copy the array to the destination. This is an efficient way of doing it. However, if I need to sort it then I will use the sort method as shown below in the snippet. This will sort alphabetically.
  1. using System.Linq;    
  2. using System;    
  3.     
  4. namespace Collections    
  5. {    
  6.     class Array    
  7.     {    
  8.         static void Main(string[] args)    
  9.         {    
  10.             string[] monthsofYear =    
  11.             {    
  12.                 "Jan",    
  13.                 "Feb",    
  14.                 "Mar",    
  15.                 "Apr",    
  16.                 "May",    
  17.                 "Jun",    
  18.                 "Jul",    
  19.                 "Aug",    
  20.                 "Sep",    
  21.                 "Oct",    
  22.                 "Nov",    
  23.                 "Dec"    
  24.             };    
  25.     
  26.             System.Array.Sort(monthsofYear);    
  27.                
  28.             foreach (string month in monthsofYear)    
  29.             {    
  30.                 Console.WriteLine(month);    
  31.             }    
  32.             Console.ReadLine();    
  33.         }    
  34.             
  35.     }    
  36. }   
sortable

However, the result is true for those collections that are sortable by nature such as arrays of noses, strings and so on. How about sorting those that are of type T? In this case IComparer<T> is useful. So, let's assume the scenario of sorting the months of the year by the length of month. Now, to do that I need to write a comparer as shown below.
  1. using System.Collections;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System;    
  5.     
  6. namespace Collections    
  7. {    
  8.     class Array    
  9.     {    
  10.         static void Main(string[] args)    
  11.         {    
  12.             string[] monthsofYear =    
  13.             {    
  14.                 "January",    
  15.                 "February",    
  16.                 "March",    
  17.                 "April",    
  18.                 "May",    
  19.                 "June",    
  20.                 "July",    
  21.                 "August",    
  22.                 "September",    
  23.                 "October",    
  24.                 "November",    
  25.                 "December"    
  26.             };    
  27.     
  28.             var comparer = new monthLengthComparer();    
  29.     
  30.             System.Array.Sort(monthsofYear,comparer);    
  31.             foreach (string month in monthsofYear)    
  32.             {    
  33.                 Console.WriteLine(month);    
  34.             }    
  35.             Console.ReadLine();    
  36.         }    
  37.             
  38.     }    
  39.     
  40.     //month length comparer    
  41.     class monthLengthComparer:IComparer<string>    
  42.     {    
  43.     
  44.         public int Compare(string x, string y)    
  45.         {    
  46.             return x.Length.CompareTo(y.Length);    
  47.         }    
  48.     }    
  49. }  
consider a scenario

Let's consider a scenario where in your input type is very big. Let's assume an array of a million items and you need to find one element out of that. Well, the most efficient way is to examine the various sorting algorithyms and implement the fastest one. However, there is one that we can use easily, the BinarySearch algorithm. This is builtin. However, there is one catch with binary search, it expects an input order in sorter order. So, my implementation will be something as in the following.
  1. using System.Collections;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System;    
  5.     
  6. namespace Collections    
  7. {    
  8.     class Array    
  9.     {    
  10.         static void Main(string[] args)    
  11.         {    
  12.             string[] monthsofYear =    
  13.             {    
  14.                 "January",    
  15.                 "February",    
  16.                 "March",    
  17.                 "April",    
  18.                 "May",    
  19.                 "June",    
  20.                 "July",    
  21.                 "August",    
  22.                 "September",    
  23.                 "October",    
  24.                 "November",    
  25.                 "December"    
  26.             };    
  27.     
  28.             System.Array.Sort(monthsofYear);    
  29.     
  30.             int indexofJuly = System.Array.BinarySearch(monthsofYear, "July");    
  31.     
  32.     
  33.             Console.WriteLine(indexofJuly);    
  34.                 
  35.             Console.ReadLine();    
  36.         }    
  37.             
  38.     }    
  39.     
  40.    }   
And it will produce the following output.

produce

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

Thanks,
Rahul