Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

Compiler Error CS0039 

Error Message

Cannot convert type 'type1' to 'type2' via a built-in conversion

A conversion with the as (C# Reference) operator is allowed by inheritance, reference conversions, and boxing conversions. For more information, see Conversion Operators (C# Programming Guide).

Example

The following example 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;

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

        //The following generates CS0039; there is no
        // built-in reference conversion from B to C.
        c = b as C;  // CS0039
    }
}

Community Additions

ADD
Show:
© 2015 Microsoft