Array Covariance Example

Array covariance. If you do much searching around the interwebz you will find that this topic is something that you just don’t do and for good reason. Click more to hear me explain in my own crazy way.. 🙂

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Covariance
{
    class Program
    {
        /// <summary>
        /// Demonstrating array covariance in an object array type.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            object[] objArray = new object[3];
            
            // This type of covariance can work with an array of object.
            objArray[0] = 3;
            Console.WriteLine("objArray [0] element: {0}", objArray[0]);

            string[] daysOfWeek = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

            object[] objArray2 = daysOfWeek;
            Console.WriteLine("objArray2 Type: {0}", objArray2.GetType());
            foreach (object item in objArray2)
            {
                Console.WriteLine(item);
            }

            // If I try and add an integer to objArray2 the compiler thinks everything is ok. However there will be an array type mismatch exception.
            // You can't add an integer to an array copy of strings. Even if it's a type of object.
            // Uncomment the next line of code to see the error.
            //objArray2[0] = 3;

            Console.ReadLine();
        }
    }
}

Array covariance is the concept that arrays allow reference conversion. This isn’t a type safe good thing because the compiler won’t notify you of the error during compile time. You will get an error during runtime or when the program gets to the point of the code.

Let me explain with the code above. I’m first declaring a string array with some elements. Then I’m declaring an object array with the values of the string array. Now this presents an interesting situation. Say some piece of code tries to add an element to the array that isn’t a string type. You can see this if you uncomment the code on line 36. Visual studio won’t report this as an error nor will the compiler report and error. However an exception will be thrown once that line of code is executed. And this is why, in most circles, array covariance is a bad thing and most of wish it wasn’t a part of the .NET framework. I will also say that there must have been a good reason for this at one time or another. Otherwise would it even still be around?

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s