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.
Example
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
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for