This documentation is archived and is not being maintained.

Compiler Warning (level 1) C4303

'cast' from 'type1' to 'type2' is deprecated, use static_cast, __try_cast or dynamic_cast

C-style type casting or function-style casting is not supported when using Managed Extensions for C++. To cast, use either dynamic_cast Operator or static_cast Operator.

The following sample generates C4303:

// C4303.cpp
// compile with: /clr /W1 /c
#using <mscorlib.dll>
__gc struct A { };
__gc struct B { };

int main() {
   B *b = new B;
   try {

   // C4303 old 'c' style cast
   A *a = (A*)b;   // C4303
   // run time exception becaused casting to incompatible types

   // use the line below to resolve the warning
   // A *a = __try_cast<A*>(b);
   } catch (System::InvalidCastException * e) {
      System::Console::WriteLine(e);
   }
}
Show: