Why are all the String Classes Sealed?

  • Thread starter Thread starter Phill
  • Start date Start date
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.
 
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
 
Back
Top