sazr
sazr

Reputation: 25938

Inserting String Into StringBuilder Causes Runtime Error

I am attempting to insert a string into a StringBuilder but I get a runtime error:

Exception of type 'System.OutOfMemoryException' was thrown.

Why is this happening and how can I fix this?

My code:

Branch curBranch("properties", "");
foreach (string line in fileContents) 
{
    if (isKeyValuePair(line))
       curBranch.Value += line + "\r\n"; // Exception of type 'System.OutOfMemoryException' was thrown.
}

Implementation of Branch

public class Branch {
    private string                      key         = null;
    public StringBuilder                _value      = new StringBuilder(); // MUCH MORE EFFICIENT to append to. If you append to a string in C# you'll be waiting decades LITERALLY
    private Dictionary <string, Branch> children    = new Dictionary <string, Branch>();

    public Branch(string nKey, string nValue) {
        key     = nKey;
        _value.Append(nValue);
    }

    public string Key {
        get { return key; }
    }

    public string Value {
        get 
        { 
            return this._value.ToString(); 
        }   
        set 
        {
            this._value.Append(value);
        }
    }
}

Upvotes: 0

Views: 223

Answers (2)

TGH
TGH

Reputation: 39278

StringBuilder sb = new StringBuilder();
foreach (string line in fileContents) 
{
    if (isKeyValuePair(line))
       sb.AppendLine(line); // Exception of type 'System.OutOfMemoryException' was thrown.
}

Try the above

I also found this explenation for why += isn't there for StringBuilder:

Why didn't microsoft overload the += operator for stringbuilder?

Upvotes: 2

Grant Winney
Grant Winney

Reputation: 66509

This line returns the entire StringBuilder contents:

return this._value.ToString();

And then you add a string to the end of the entire previous contents:

curBranch.Value += line + "\r\n";

And append it here:

this._value.Append(value);

Your StringBuilder is going to get huge very quickly, because every time you call the "setter" you're placing a copy of the entire contents into it again.


What you might consider instead is exposing the StringBuilder via your property:

public StringBuilder Value
{
    get { return this._value; }   
}

Then just use it like this:

curBranch.Value.AppendLine(line);

Upvotes: 5

Related Questions