开发者

Can i make string behave like Reference Type?

In C#, strings are reference type but behaves like value type. e.g.

string str = "I am a string";
str.Replace("am", "was");  //str still contains same value, 
//i want it to make "I was a string"

I know i can do this by

str = str.Replace("am", "was");

But i don't want to re-assign it. Is there anyway to make them behave like Reference type?

Additional:

i am having a key-value pair collection(not dictionary) and i want to prefix some text to its key(string), right now i am removing the key-value pair and then creating a new using the same value and adding that in to the collection. i don't want to do this as this doesn't seem to be a right way. I want to update the Key directly.

Edit:

I might get two collection from Model with the same keys having different values in each collection. so inside View-Model i want to alter the keys by prefixing them with a value for first and second collec开发者_如何学编程tion separately. And will make all keys unique.


Use System.Text.StringBuilder.

If you can't use StringBuilder try to read here http://www.codeproject.com/KB/dotnet/strings.aspx

See "Direct Modifications of Strings".


Your options are basically:

  • leave well alone (highly recommended)
  • create your own string class (why? hilarity will ensue if other developers downstream don't know your intentions)
  • use the StringBuilder to concatenate strings together in a memory light fashion (probable solution to most your issues)


No, strings are meant to be immutable (unchange-able). If you need to constantly append to a string, use System.Text.StringBuilder (MSDN) and use the Append method.

var sb = new StringBuilder();
sb.Append("I");
sb.Append(" am");
sb.Append(" still");
sb.Append(" a string");
Console.WriteLine(sb.ToString());


No, strings are immutable (once created, you cannot change them). However, if you really (I can't imagine a situation where this would be acceptable for me) need this, you can create your own class (let's name it MutableString), which would expose similar public API to string, but will handle those reassignments internaly, and will implicitly cast from/to string.

EDIT: Remember to override GetHashCode() and Equals(), otherwise your collection will not work. However - it's recommended that hash code remained constant - so basically, all you can resonably do is to return 0 for every MutableString. This WILL be a performance issue if you use this as a key in collections that rely on hashcode (like Dictionary) - be aware of that.


You could just create a helper method that updates the string for you e.g.

public static class StringHelper
{
    public static void Replace(ref string originalStr, string strToReplace, string replaceStr)
    {
         originalStr = originalStr.Replace(strToReplace, replaceStr);
    }
}

Usage

var str = "I am a string";
StringHelper.Replace(ref str, "am", "was");


Not directly. You'll need to create your own class that in turn contains a string and that has your own methods to handle Replace and suchlike.

The inevitable question is - why? In what situations are you concerned that string does not behave as it does?


No, and you shouldn't need to.

Better take a step back and show us the problem you are trying to solve with mutable strings.

Edit, from a comment:

i am having a key-value pair collection(not dictionary) and i want to prefix some text to its key(string), right now i am removing the key-value pair and then creating a new using the same value and adding that in to the collection. idon't want to do this as this doesn't seem to be a right way. I want to update the Key directly.

You are seeing a problem where there isn't one. Just use the s = s.Replace() pattern. It is the right (and only) way.

Using a StringBuilder is possible but won't help much, it only addresses the performance of the actual replace.

If you want to speed it up, you will have to show the API for your collection class. It may be possible to substitute just the key in a Key/Value pair.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜