...which might not actually be a variance issue, after all. When I compiled my code, Visual Studio would give me the following error:
Type of conditional expression cannot be determined because there is no implicit conversion between 'ClassA' and 'ClassB'
I read up on this error here, and it sure sounded like it's not possible to use abstract classes as contracts like interfaces, where I can use derived classes in place of their base class. To make things simpler, I wrote some test classes that mimic the relationship of my actual code. Please consider the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractClassImplicitConversion {
    class Program {
        static void Main(string[] args) {
            StartClass temp = new StartClass();
        }
    }
    public class StartClass
    {
        public AbstractClass _ac { get; private set; }
        public StartClass()
        {
            bool which = true;
            // this block will compile
            /*
            if( which)
                _ac = new DerivedClass1();
            else 
                _ac = new DerivedClass2();
             */
            // this block will not compile
            _ac = which ? new DerivedClass1() : new DerivedClass2();
        }
    }
    public interface SomeInterface
    {
        void SiFoo();
    }
    public abstract class InnerAbstractClass
    {
        public abstract void IacFoo();
    }
    public abstract class AbstractClass : InnerAbstractClass, SomeInterface
    {
        public override void IacFoo() {
        }
        public void SiFoo() {
        }
        public abstract void OverrideMe();
        public abstract void OverrideMeToo<T>();
    }
    public class DerivedClass1 : AbstractClass
    {
        public override void OverrideMe() {}
        public override void OverrideMeToo<T>() {}
    }
    public class DerivedClass2 : AbstractClass
    {
        private int _blah;
        public override void OverrideMe() {}
        public override void OverrideMeToo<T>() {}
    }
}
The problem is with the line:
_ac = which ? new DerivedClass1() : new DerivedClass2();
If I use an inline if, I'll get that dreaded compiler error. However, if I instead use this:
if( which)
    _ac = new DerivedClass1();
else 
    _ac = new DerivedClass2();
my code compiles fine.
Can anyone explain what causes this? I've always considered the two to be equivalent. Thanks!
 
     
    