BUILD 2014 – Day 1 Keynote

BUILD 2014 – Day 1 Keynote – My notes.  (Complete with tons of screengrabs).

#1,066 – Constraining a Type Parameter on a Generic Interface

You can constrain a type parameter on a generic interface so that it can be used only as the output type of methods in the interface and not as a type of any method parameters.  You do this using the out keyword on the type parameter.

        public interface FirstAndLast<out T>
        {
            T First();
            T Last();
            void AddThing(T item);   // won't compile
        }

This seems like an odd thing to do, but ensuring that a particular type parameter is used only as output allows us to later use this generic interface covariantly.  (I’ll explain this in an upcoming post).

#1,065 – Cases When Array Covariance Doesn’t Work

Array covariance in C# allows you to assign an array of objects of a more derived type to an array of objects of a base type.  An array of type B can be assigned to an array of type A is type B is implicitly convertible to A.  More specifically, an implicit reference conversion from B to A must exist.

This restriction means that array covariance will not work for implicit conversions that are not implicit reference conversions.  Specifically, arrays are not covariant for implicit numeric conversions, boxing conversions or implicit custom conversions.

            // Covariance does work when implicit reference conversion
            // exists for underlying types
            //   - From S to T when S derives from T
            //   - From S to object or dynamic
            //   - From S to interface-type T when S implements T
            //   - From interface-type S to interface-type T when S derives from T
            // All examples below work (compile)
            Terrier[] tarr1 = new Terrier[2];
            Dog[] darr1 = tarr1;
            object[] oarr1 = tarr1;
            dynamic[] dynarr1 = tarr1;
            Dog[] darr2 = new Dog[2];
            IBark[] ibarr1 = darr2;
            IBarkBetter[] ibb1 = new IBarkBetter[2];
            IBark[] ibarr2 = ibb1;

            // Covariance does NOT work with implicit numeric conversions
            //  e.g. byte to short
            byte[] barr1 = new byte[2];
            short[] sarr1 = barr1;  // Compile-time error

            // Covariance does NOT work with implicit nullable conversions
            int[] inarr1 = new int[2];
            int?[] innarr1 = inarr1;  // Compile-time error

            // Covariance does NOT work with implicit boxing conversions
            int[] inarr2 = new int[2];
            object[] oarr2 = inarr2;  // Compile-time error

            // Covariance does NOT work with implicit custom conversions
            Cow c = new Cow("Bessie");
            Dog d = c;  // Implicit Cow to Dog works (custom conversion)
            Cow[] herd = new Cow[2];
            Dog[] pack = herd;  // Compile-time error