This documentation is archived and is not being maintained.

Compiler Error CS0039

Cannot convert type 'type1' to 'type2'

A conversion with the as operator is only allowed by inheritance.

The following sample generates CS0039:

// CS0039.cs
using System;

class A
{
}

class B: A
{
}

class C: A
{
}

class M
{
   static void Main()
   {
      A a = new C();
      B b = new B();
      C c;

      c = a as C;  // valid, there is a built-in reference
                   // conversion from A to C.

      c = b as C;  // CS0039, there is no built-in reference
                   // conversion from B to C.
   }
}
Show: