StringBuilder AppendIf

namespace Utils
{
    using System.Text;

    public static class Extensions
    {
        public static StringBuilder AppendIf(this StringBuilder builder, bool condition, string value)
        {
            if (condition) builder.Append(value);
            return builder;
        }
    }
}
Example:
var keyBuilder = new StringBuilder();

keyBuilder.AppendIf(ctrl, "[ctrl]")
    .AppendIf(shift, "[shift]")
    .AppendIf(alt, "[alt]")
    .Append(" ")
    .Append(key);

Description

Makes it possible to conditionally append to a StringBuilder while keeping it fluent

Details

Double click on the code to select all.

 

;