Hi all,
I'm having trouble understanding why arrays in C# are covariant and what benefits this covariance can bring. Consider the following trivial code example:
object[] myArray = new string[1];
myArray[0] = 1;
This code will compile okay, but will unceremoniously and perhaps unsurprisingly explode at runtime.
If I try to attempt this same thing using generics, the compiler would grumble at me and I would realise my stupidity at an early stage, so my question is this: Why does the C# compiler allow this covariance with arrays and furthermore, what are the potential benefits?