I have a class with a dictionary object. Any class that derives from that class, I want to override the dictionary with it’s own implementation.
How can this be achieved as the virtual keyword is not valid here?
Thanks.
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.
You can’t have virtual fields, but you can have virtual properties.
Additionally, it would be a good idea to declare the property-type to be of the
IDictionary<TKey, TValue>interface rather than theDictionary<TKey, TValue>concrete- type, since this class is not designed for inheritance.E.g:
Subclasses will not see the field; only the property will be visible. They are free to override the property and provide their own implementation; for example by returning an instance of a custom-type that implements the interface.