I have a 2 classes one is base class and second is derived class.
Want to restrict object slicing, how can I do it using C#.net ?
I have a 2 classes one is base class and second is derived class.
Share
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.
UPDATE:
My initial thoughts as described below were found as not true:
See: C++ slicing in Java / C#
Original Answer:
If I understood correctly this is a theoretical question.
REDUCINGslicing can be done by not defining new members in the derived class.slicing occurs when assigning an instance of a derived class to a base class reference type.
In this case the system does not know where to allocate a new data-type because it is defined in the derived class only and not in the base class.
But there is no way to
RESTRICTthat, this is what suppose to happen and it is defined asObject slicing – Wikipedia
however you can declare a class as
sealedto prevent inheritance that will restrict object slicing because it will prevent the cause (Inheritance) so you will not have a derived class to assign to a base reference by value.