473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

string is sealed

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...
Nov 16 '05 #1
3 4893
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris C" <cm****@yahoo.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
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...

Nov 16 '05 #2
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/<et**************@TK2MSFTNGP10.phx.gbl>

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]
Nov 16 '05 #3

"Chris C" <cm****@yahoo.com> wrote in message news:et**************@TK2MSFTNGP10.phx.gbl...
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
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bharat Karia | last post by:
Hi, Is it possible to writed Sealed classes in C++ . i.e. there is no sealed/final keyword in C++, but is it possible to achieve the same effect? i.e. deriving from a sealed class is an error...
8
by: Nader | last post by:
Hello all, In C# string is a reference type but I learned that string is different from other reference types such as class. For example, if you pass a string argument to a method and then...
14
by: Zeng | last post by:
Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance? Thanks!
4
by: Mike P | last post by:
What is the equivalent of setting a global connection string in Web.Config in WinForms? Any help would be really appreciated. Cheers, Mike
9
by: ad | last post by:
I have a string like "1,2,2,3,3,3,4" I want to trim off the duplicate part, and make it to "1,2,3,4" How can I do?
2
by: Soeren S. Joergensen | last post by:
Hi, I need to have something like (wich as written of course is not possible) public enum CountryType : String { USA = "US", UnitedKingdom = "UK", Germany = "DE", Denmark = "DK"
10
by: TJM | last post by:
Hi, Is it possible to have a method sealed and abstract at the same time? MSDN states clearly that this is not allowed for classes but it does not mention it for methods. I tried with a simple...
18
by: Vedo | last post by:
ref struct XXX abstract sealed { literal int A = 5; }; The definition above gives me the compiler warning "C4693: a sealed abstract class cannot have any instance members 'A'". The equivalent...
35
by: Smithers | last post by:
I have been told that it is a good idea to *always* declare string variables with a default value of string.Empty - for cases where an initial value is not known... like this: string myString =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.