string is sealed

  • Thread starter Thread starter cmrchs
  • Start date Start date
C

cmrchs

Hi,

why is the String class sealed ?

wouldn't it be nice to have a

class MyString : String
{ ... }

containing already all the nice features of String ?

thnx
Chris

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
Chris,

I can see why this is the case. Given that strings are such an
important part of the framework, they need to guarantee that the internal
mechanisms are not compromised.

If you need to, create a utility class that will perform operations on
an input string, and return a new string.

Hope this helps.
 
The problem is that if you could inherit from string you could, in effect, change its semantics. Strings in .NET are immutable. With derivation allowed it would be possible to create strings that were not. This would cause unexpected side-effects

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Hi,

why is the String class sealed ?

wouldn't it be nice to have a

class MyString : String
{ ... }

containing already all the nice features of String ?

thnx
Chris

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004



[microsoft.public.dotnet.languages.csharp]
 
Chris C said:
Hi,

why is the String class sealed ?

wouldn't it be nice to have a

class MyString : String
{ ... }

containing already all the nice features of String ?

thnx
Chris

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...

From MSDN:
<quote>
The sealed modifier is primarily used to prevent unintended derivation, but it also enables certain run-time optimizations. In
particular, because a sealed class is known to never have any derived classes, it is possible to transform virtual function member
invocations on sealed class instances into non-virtual invocations.
</quote>

So it has some performance benefits as well, in addition to what the previous posters already said.

Hans Kesting
 
Back
Top