Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
String is a sealed type in c# and hence cannot be derived from. Do you
agree?
Posted
Updated 13-Apr-13 2:14am
v2
Comments
[no name] 13-Apr-13 8:14am    
Since the string class is sealed there is nothing to agree with. It is just a statement of fact.

Yes: MSDN[^]
 
Share this answer
 
You cannot derive from the string class[^]. As you mentioned it is sealed.
However, by using extension methods, you can actually work around to add more functionality to the string class.
 
Share this answer
 
If you are sure you need to extend some functionality of string, then extension methods are a good option for the string class.

Inheritance is often abused, especially in C++ which doesn't have a concept of sealed. If you are developing a class X, be very sure that it X is a string, rather than X is implemented in terms of string. The tight coupling of client code of X to string can cause problems down the road. The sealed keyword is actually a safeguard against this. More often than not it is a useful protection from deriving business classes from lower level classes. Business classes are almost always implemented in terms of lower level classes.

I'll be honest and say this is something that was only recently pointed out to me in the context of C++ development, but I would say the same applies to most languages. Inheritence is one of the most strongest relationship between classes that one can form as pointed out be Herb Sutter: Uses and Abuses of Inheritance[^]. Again, C++ references, but probably relevant to this question.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900