.NET strings are not always immutable!

Strings are immutable. If you want to modify a sequence of characters, use StringBuilder. At least, that’s whats officially said. But in the framework there is at least one method that does modify a string:

TextRenderer.MeasureText() with ModifyString and EndEllipses will modify your string to match the ellipsed text if ellipsing happens. You can look at this VB# example on codeproject using TextRenderer.MeasureText() for trimming text on how it is used.

The string seems to be modified directly in native code by DrawTextEx from user32.dll. Additionally to the scary fact that strings are not immutable, the length of the string is not updated, regardless if the resulting string is shorter!

For instance if you have a string “aaaaaaa” which will be truncated to “aa...“, the Length property will still return 7 for the shortened string. The debugger shows that the string will in fact be “aa…\0a” after the operation. So maybe it might be right that the string is still 7 characters long but most outputting functionality like Console.Out.WriteLine() gets confused sometimes and stops any further output to the debugger or console under certain conditions.

A very quick investigation of the System.Drawing assembly using Lutz Roeder’s fabulous .NET Reflector showed that at least there should be no memory corruption in case “WW” would get ellipsed to “W...“, as DrawTextEx takes the length of the buffer and should result only in “W.“.

Summing up, I find the corruption of an immutable string by an official Microsoft API very troubling.

VisualStudio.NET: Text-Editor Guide at 80

Guiding lines in VS.NET 2005It is still considered good style to keep code-lines within a certain bound (e.g. 80 characters). IDEs like Eclipse offer to display a red guiding line at the chosen offset to help developers keep within this bound.

By modifying the registry a similar guide can be enabled for Visual Studio 2003 / 2005. Add a string value Guides to the key (VS.NET 2005, VS 2003 has version 7.1)

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0\Text Editor.

Set the value to

RGB(128,0,0) 80

To have multiple guides (like in the screenshot), add the additional columns space delimited, e.g.

RGB(128,0,0) 80 100

Ackn.: This information has been provided by Hannes Pavelka in Microsoft’s newsgroup microsoft.public.dotnet.languages.csharp, Message-ID: <e1jag7$qk7$02$1@news.t-online.com> (Article in Google Groups):

Microsoft: Shared Source Common Language Infrastructure 2.0 Release

Just came accross this: seems like Microsoft has released some parts of the CLI under one of their “free” licenses.

Download details: Shared Source Common Language Infrastructure 2.0 Release

Update 2006/03/26: As I just noticed at Mono’s “Contributing” page, they won’t accept any contributions from people who had a look at the download.

IKVM.NET: Interaction between C# and Java

A nice project, everyone coming from Java and migrating to C#:
IKVM.NET Home Page

It is a JVM implemented in .NET, contains a .NET implementation of a lot classes from the Java class libraries (JDK), compliance of 1.4 almost complete and contains tools for interop between Java and .NET.