Re: StringBuilder
mikeb wrote:
[color=blue]
> Alvin Bruney wrote:
>[color=green][color=darkred]
>>> Not sure exactly which construct you mean here, to be honest.[/color]
>>
>>
>> was talking about .append(blah + moreblah) as opposed to .append(blah);
>> .append(morebla h)
>>
>>[color=darkred]
>>> It says that "literal1"+"lit eral2" is always evaluated at compile-time
>>> to "literal1litera l2".[/color]
>>
>>
>>
>> ok well that seems to be good for me then since it evaluates it at
>> compile
>> time. but what happens at run-time? what if literal2 was a string object
>> (not interned) - as would frequently be the case. does it know what to do
>> then?
>>
>> consider StringBuilder1. Append("my name is" + varName). Knowing this
>> answer
>> is fundamentally important to me since I am building about 10,000 queries
>> that way and performance must always supercede anything else.[/color][/color]
You might also want to test StringBuilder.A ppendFormat() - it may be
smart enough to avoid the transient string object creation (probably
not, though).
[color=blue]
>
>
> If the transient string objects that are created in calls like this are
> an issue, there are a couple of ways to deal with the problem, while
> keeping readability:
>
> 1) remember that StringBuilder.A ppend() returns the StringBuilder
> instance. So you can make the above call like so:
>
> StringBuilder1. Append( "my name is ").Append( varName);
>
> whether this remains readable or not is a question of taste. It
> does not create the transient string object, however.
>
> 2) write your own utility method that takes multiple strings to
> append to a StringBuilder instance. Since StringBuilder is sealed, this
> would likely be a static class - something like (untested code follows):
>
> public class SbUtil {
> public static StringBuilder Append( StringBuilder sb,
> string s1,
> string s2)
> {
> sb.Append( s1);
> sb.Append( s2);
> return( sb);
> }
> }
>
> // ...
>
> SbUtil.Append( StringBuilder1, "may name is ", varName);
>
>[color=green]
>>
>> "Jon Skeet" <skeet@pobox.co m> wrote in message
>> news:MPG.19d4b8 f04604efcc9896f 7@news.microsof t.com...
>>[color=darkred]
>>> Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:
>>>
>>>> well for starters, i got this kind of construct peppered all over my[/color]
>>
>>
>> code. i
>>[color=darkred]
>>>> start off with good intentions with the append but there seems to
>>>> always[/color]
>>
>>
>> be
>>[color=darkred]
>>>> a piece of string needing to be added and not worth the starting a new[/color]
>>
>>
>> line.
>>[color=darkred]
>>>> i know that is so lazy. always figured the language was smart enough to[/color]
>>
>>
>> know
>>[color=darkred]
>>>> better.
>>>
>>>
>>> Not sure exactly which construct you mean here, to be honest.
>>>
>>>
>>>> i didn't care to read spec 14.15. what does it say in the queens[/color]
>>
>>
>> english? do
>>[color=darkred]
>>>> i need to stop doing that (is really what i am asking)
>>>
>>>
>>> It says that "literal1"+"lit eral2" is always evaluated at compile-time
>>> to "literal1litera l2".
>>>
>>> --
>>> Jon Skeet - <skeet@pobox.co m>
>>> http://www.pobox.com/~skeet
>>> If replying to the group, please do not mail me too[/color]
>>
>>
>>
>>[/color]
>
> --
> mikeb
>[/color]
--
mikeb
mikeb wrote:
[color=blue]
> Alvin Bruney wrote:
>[color=green][color=darkred]
>>> Not sure exactly which construct you mean here, to be honest.[/color]
>>
>>
>> was talking about .append(blah + moreblah) as opposed to .append(blah);
>> .append(morebla h)
>>
>>[color=darkred]
>>> It says that "literal1"+"lit eral2" is always evaluated at compile-time
>>> to "literal1litera l2".[/color]
>>
>>
>>
>> ok well that seems to be good for me then since it evaluates it at
>> compile
>> time. but what happens at run-time? what if literal2 was a string object
>> (not interned) - as would frequently be the case. does it know what to do
>> then?
>>
>> consider StringBuilder1. Append("my name is" + varName). Knowing this
>> answer
>> is fundamentally important to me since I am building about 10,000 queries
>> that way and performance must always supercede anything else.[/color][/color]
You might also want to test StringBuilder.A ppendFormat() - it may be
smart enough to avoid the transient string object creation (probably
not, though).
[color=blue]
>
>
> If the transient string objects that are created in calls like this are
> an issue, there are a couple of ways to deal with the problem, while
> keeping readability:
>
> 1) remember that StringBuilder.A ppend() returns the StringBuilder
> instance. So you can make the above call like so:
>
> StringBuilder1. Append( "my name is ").Append( varName);
>
> whether this remains readable or not is a question of taste. It
> does not create the transient string object, however.
>
> 2) write your own utility method that takes multiple strings to
> append to a StringBuilder instance. Since StringBuilder is sealed, this
> would likely be a static class - something like (untested code follows):
>
> public class SbUtil {
> public static StringBuilder Append( StringBuilder sb,
> string s1,
> string s2)
> {
> sb.Append( s1);
> sb.Append( s2);
> return( sb);
> }
> }
>
> // ...
>
> SbUtil.Append( StringBuilder1, "may name is ", varName);
>
>[color=green]
>>
>> "Jon Skeet" <skeet@pobox.co m> wrote in message
>> news:MPG.19d4b8 f04604efcc9896f 7@news.microsof t.com...
>>[color=darkred]
>>> Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:
>>>
>>>> well for starters, i got this kind of construct peppered all over my[/color]
>>
>>
>> code. i
>>[color=darkred]
>>>> start off with good intentions with the append but there seems to
>>>> always[/color]
>>
>>
>> be
>>[color=darkred]
>>>> a piece of string needing to be added and not worth the starting a new[/color]
>>
>>
>> line.
>>[color=darkred]
>>>> i know that is so lazy. always figured the language was smart enough to[/color]
>>
>>
>> know
>>[color=darkred]
>>>> better.
>>>
>>>
>>> Not sure exactly which construct you mean here, to be honest.
>>>
>>>
>>>> i didn't care to read spec 14.15. what does it say in the queens[/color]
>>
>>
>> english? do
>>[color=darkred]
>>>> i need to stop doing that (is really what i am asking)
>>>
>>>
>>> It says that "literal1"+"lit eral2" is always evaluated at compile-time
>>> to "literal1litera l2".
>>>
>>> --
>>> Jon Skeet - <skeet@pobox.co m>
>>> http://www.pobox.com/~skeet
>>> If replying to the group, please do not mail me too[/color]
>>
>>
>>
>>[/color]
>
> --
> mikeb
>[/color]
--
mikeb
Comment