This code raises an exception at run time because of an invalid cast
using System;
public class Starter {
public static void Main() {
MyClass obj = new MyClass();
// Fails at compile time
// YClass alias=obj;
// Fails at run time
YClass alias = (YClass)obj;
obj.MethodA();
obj.MethodB();
}
}
public class MyClass {
public virtual void MethodA() {
}
public virtual void MethodB() {
}
}
public class YClass : MyClass {
public override void MethodA() {
}
}
Related examples in the same category