Implicit Type Conversion and Arrays
I came across something I found sort of interesting a week or two ago. I had an array of integers that I wanted to use in populating a ComboBox for a tool I was working on. I started to call the ComboBox.Items.AddRange method which simply takes an array of objects. To my surprise I received the wonderful "The best overloaded method match for 'System.Windows.Forms.ComboBox.ObjectCollection.AddRange(object[])' has some invalid arguments. Hmm, strange. Doesn't everything in .NET derive from object? I quickly opened my favorite playground console application (where I do most of my quick coding tests) and starting poking around with objects, object arrays and how type conversions between them works.
First I performed a sanity check:
int i = 5;
object o = i;
Sanity confirmed. No complaints from the compiler. Integers can indeed be implicitly converted to objects because they inherit from object.
Now let's look at arrays:
int[] iArray = new int[0];
object[] oArray = iArray;
Ugh, low and behold this produces the dreaded squiggly line in Visual Studio with the message "Cannot implicitly convert type 'int[]' to 'object[]'. Say what?
OK, how about this then.
int[] sArray = new string[0];
object[] oArray = sArray;
Again no problem. Implicit conversion of a string array to an object array is just fine.
So any type can be converted to object but when it comes to an array of types only arrays of some types can be converted to object arrays? What's going on here? The answer comes when you realize that int is a struct and thus a value type while string is a class and thus a reference type. The .NET compiler will handle implicit conversion of int to object through boxing. However, to convert int[] to object[] it would have to read into the array of integers and box each element individually and at compile time the number of elements of the array is unknown so that just isn't possible.
Of course the original issue I was working on was easily solved by using LINQ's Select method to yield an array of strings from my array of integers but I found the issue interesting so here I am.