473,396 Members | 1,987 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,396 software developers and data experts.

Why are all the String Classes Sealed?

Why can't we add new methods to String or StringBuilder?
Nov 16 '05 #1
2 2813
Phill,

From a language perspective, I would imagine that while it is a
reference type, it is also a primitive type of sorts, and therefore, should
be sealed. From a more practical standpoint, I assume there are a number of
optimizations that take place in the use of the string which they don't want
touched in a class derivation.

There is nothing to prevent you from creating another class with helper
methods that work on strings.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Phill" <wa********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
Why can't we add new methods to String or StringBuilder?

Nov 16 '05 #2
Had a similar problem where I wanted to add methods to a string property.

My solution looks a little like this.

Create a new class like so:
public class MyString
{

public bool IsRequired()
{
// do some stuff
return true;
}

private string m_val;
// new property to accept string
public string Value
{
get{return m_val;}
set{m_val = value;}
}

// implicit conversion operator
public static implicit operator MyString(string str)
{
MyString myStr = new MyString();
myStr.Value = str;
return myStr;
}
}

And here's some code showing it's use.

MyString name = new MyString;
name = "Scott"
name.IsRequired();

string FullName = name.Value + "B";
Hope that helps...

--Scott

"Phill" <wa********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
Why can't we add new methods to String or StringBuilder?

Nov 16 '05 #3

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...
4
by: Néstor Marcel Sánchez Ahumada | last post by:
In a method declaration the 'sealed' keyword must be used with the 'override' keyword to avoid further overriding. Thus it can't be used in base classes. Why? This would be a good enhancement for...
3
by: cmrchs | last post by:
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 ?
6
by: ESPN Lover | last post by:
I'm fairly new to the whole .NET and OOP programming so bear with me. In the past if I wanted to have constants defined that I could use in my code, I'd include them either the file or as an...
13
by: Mark Rae | last post by:
Hi, Since sealed classes can't be instantiated with the new keyword e.g. CClass objClass = new CClass(), does this mean that they don't have constructors / deconstructors or, if they do, that...
13
by: Rob Meade | last post by:
Hi all, I have written a small ProperCase function which I would like to make available to our team at work through our common class library. A colleague mentioned that I could write a new...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
7
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to create a generic class which will take a command string and I can issue statement like this: Dim rdrCustomers As New Generic_Reader("select * from customers") However, I have not...
9
by: dylan.miller | last post by:
I'm having trouble understanding the internal access modifier. There are many classes in my assembly that should not be accessible outside of the assembly. I've used the internal access modifier...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.