Playing with strings (part 1 of n)
I was writing a method today, one that has to concatenate and format 2 strings. As every other developer on the planet, I’m using
System.Text.StringBuilder class when I work with strings (for concatenating and formatting – because that’s what we’re using it for).
There’s also a very well known static method of
System.String class called
string.Format(string, string).
It’s being used when you want to do a quick and dirty string formatting, without having to initialize a new instance of
System.Text.StringBuilder.
So today, I’ve asked myself: What does the string.Format(string, string) use internally?
There are 3 ways of getting an answer on that question.
1. Becoming a MVP and then becoming a member of the Shared Source Initiative program, under a very special NDA. That gives you access to the source of the Windows OS, and .NET as a part of it. After that you can’t discuss what you’ve seen with people outside that program. So writing a blog post like this one is not an option
2. Doing some refactoring with Resharper. It’s one of the best tools out there, when it comes to trying to find out what’s going on under the covers of any .NET assembly that haven’t been obfuscated.
3. Getting the source of SSCLI implementation of .NET. SSCLI is an open source initiative by Microsoft, that represents a good portion of the .NET Framework. Microsoft has released the v2 of SSCLI a couple of months ago, so it’s publicly available. While there is no guarantee that what you’ll see will be the same thing running in .NET 1.0/1.1/2.0, but it’s the closest thing you have. And it has the source of the CLR implementation of SSCLI along with some of the tools you get with .NET Framework SDK – something you can’t get to work with Resharper – most of those are native code.
You can get the SSCLI here: http://www.microsoft.com/downloads/details.aspx?FamilyId=8C09FD61-3F26-4555-AE17-3121B4F51D4D&displaylang=en.
At Tam Tam, we have the SSCLI under our source control database (we use Vault for source control system – it has very comfortable Web UI for accessing your sources).
So I’ve opened the repository were I’ve put the SSCLI into a few weeks ago, and found the implementation of the before mentioned method.
Here it is:
public static String Format(String format, Object arg0) {
return Format(null, format, new Object[] {arg0});
}
public static String Format(String format, Object arg0, Object arg1) {
return Format(null, format, new Object[] {arg0, arg1});
}
public static String Format(String format, Object arg0, Object arg1, Object arg2) {
return Format(null, format, new Object[] {arg0, arg1, arg2});
}
public static String Format(String format, params Object[] args) {
return Format(null, format, args);
}
public static String Format( IFormatProvider provider, String format, params Object[] args) {
if (format == null || args == null)
throw new ArgumentNullException((format==null)?"format":"args");
StringBuilder sb = new StringBuilder(format.Length + args.Length * 8);
sb.AppendFormat(provider,format,args);
return sb.ToString();
}
The important one is the last – the others are simply calling it with some default parameters.
It turns out that the method itself uses System.Text.StringBuilder. So it’s safe to use it, when we have a simple concatenation or formatting and concatenation cases. Like the one I had.