StringBuilder.Clear() and Windows Phone 7 and .Net 3.5 compatibility

Jan 28, 2011 at 8:58 AM



I am using the .Net 3.5 fork of Caliburn and noticed two constructs like these in the source code:


#if WP7
                                builder = new StringBuilder();


Problem is, StringBuilder.Clear() is new in .Net 4.0 and won't compile in the .Net 3.5 fork.

May i suggest an alternative? Just replace with:

       builder.Length = 0;

That works for all platforms and is as efficient as .Clear()



Jan 28, 2011 at 9:13 AM
Edited Jan 28, 2011 at 9:14 AM

I decided to use Clear() since I was sure that it would not cause the internal array to be resized or re-allocated, but it didn't occur to me that the function was just syntactic sugar... infact, I checked with Reflector and... 

public StringBuilder Clear()
    this.Length = 0;
    return this;

so it si better to use the method you proposed... we can avoid a method call and support all versions without compile switches.


Jan 28, 2011 at 9:56 AM

What would we do without Reflector? :)


Jan 28, 2011 at 9:58 AM
Edited Jan 28, 2011 at 9:59 AM

Hehe, indeed! :)


I'll add the work item to keep track of the issue.

Jan 28, 2011 at 12:31 PM

Fixed in 806081a50440 

Mar 3, 2011 at 4:33 AM

Hey did you get the issue with using Zip generics?

Mar 4, 2011 at 8:33 PM

There is an implementation in there which is conditionally compiled for WP7. You could simply add another conditional symbol and set your build to include it for 3.5