I used a StringBuilder to write an SQL query in .NET. After I perform the query, is there a point in manually setting the contents of the string to ""?
I'm guessing this is already being done by the compiler (maybe only with the optimize option enabled).
In C#, you cannot set the contents of a string to "". You cannot set the contents of a string to anything. Strings in C# are immutable.
Perhaps what you meant to say is "is there a point in clearing the StringBuilder?"
In theory, there could be merit in clearing the StringBuilder, if:
StringBuilderaround and reusing it.Under the above scenario, it might be worth clearing the
StringBuilderafter each use, instead of immediately before each reuse.But you are probably not keeping the
StringBuilderaround, and even if you are, your SQL queries are probably not megabytes long, so stop worrying about this.And no, your guess is wrong, the compiler will not do anything about any of this, either with or without optimizations. These are all affairs of the runtime, not of the compiler.