public static void Main()
var output = new StringBuilder();
output.AppendIfNotNull( 0, v => string.Format( "I got added with value {0}!", v ) );
output.AppendIfNotNull<object>( null, v => "I didn't get added" );
output.AppendIfNotNull( string.Empty, v => "I did get added" );
Console.WriteLine( output.ToString() );
public static class StringBuilderExtension
public static void AppendIfNotNull<TValue>(this StringBuilder sb, TValue value, Func<TValue, string> transform)
sb.AppendLine( transform( value ));