Compiler Error CS0039

Cannot convert type 'type1' to 'type2' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

A conversion with the as operator is allowed by inheritance, reference conversions, and boxing conversions.


The following example generates CS0039:

using System;

class A { }
class B : A { }
class C : A { }

class Example
    static void Main()
        C c;

        // This compiles, because
        // there is an explicit reference conversion from type A to type C.
        A a = new C();
        c = a as C;

        // This generates CS0039, because
        // there is no implicit or explicit reference conversion between B and C types.
        B b = new B();
        c = b as C;  // CS0039