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

Jan 28, 2011 at 9:58 AM

Hi,

 

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();
#else
                                builder.Clear();
#endif

 

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()

 

Thanks,
          Gunter

Jan 28, 2011 at 10:13 AM
Edited Jan 28, 2011 at 10: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 10:56 AM

What would we do without Reflector? :)

Thanks!

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

Hehe, indeed! :)

P.S.

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

Coordinator
Jan 28, 2011 at 1:31 PM

Fixed in 806081a50440 

Mar 3, 2011 at 5:33 AM

Hey did you get the issue with using Zip generics?

http://caliburnmicro.codeplex.com/discussions/219874

Coordinator
Mar 4, 2011 at 9: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