C# – Casting an object to an interface-Collection of common programming errors
Suppose we have an interface:
interface ICustomShape
{
}
and we have a class that inherits from the Shape class, and implements the interface:
public class CustomIsocelesTriangle : Shape, ICustomShape
{
}
How would I go about casting a CustomIsocelesTriangle to a ICustomShape object, for use on an “interface level?”
ICustomShape x = (ICustomShape)canvas.Children[0]; //Gives runtime error: Unable to cast object of type 'program_4.CustomIsocelesTriangle' to type 'program_4.ICustomShape'.