class preson
{< br> Public int I = 30 ;
Public int N = 40 ;
Public int Add ( int A, int B)
{< br> return A + B;
}< br> class water: preson
{< br> Public int cut ( int B, int C)
{< br> return B-C;
}< br>
}
ClassProgram
{
Static VoidMain (String[] ARGs)
{
Preson P =NewWater (); // can be converted to the parent type
P. Add (2,4);
Water S = (water) New preson (); // the code can be compiled. An error occurs during runtime and the parent type cannot be converted to the child type. Invalidcastexception Error
Water W =NewWater (); // After conversion, all common methods, fields, and attributes of the parent class can be accessed.
W. Cut (2,3);
Bool isbol = (W is preson); // The is Operator returns the bool value. No exception is thrown. True is returned here.
Preson pp = new preson ();
Bool isbol = (PP is water); // it won't work here
preson Pr = w as preson; // CLR checks whether W is compatible with the preson type. If yes, a non-null reference of the same object is returned. Otherwise, null is returned. The as operator works in the same way as forced type conversion, but it does not throw an exception. If the object cannot be converted, the result is null.
}< BR >}