Why I can’t use a class that inherits a Shapes class?
I need to extend the Rectangle class with some methods, but i want to use this class in the same way I use a Shape, how can I do?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As Jon pointed out, Rectangle is sealed.
Depending on what you’re trying to do, a couple options are:
You can extend Shape with your own class that contains Rectangle, and augment the functionality through composition. These objects wouldn’t be considered Rectangles from an “is” check.
You can write extension methods for Rectangle, and then you can use them on any Rectangle. Then the objects would still be considered Rectangles.
e.g.