FormatIf

public static string FormatIf<T>(this T value, string format) where T : struct {
 return FormatIf(value, null, format, null);
}

public static string FormatIf<T>(this T value, string format, string defaultText) where T : struct {
 return FormatIf(value, null, format, defaultText);
}

public static string FormatIf<T>(this T value, Func<T, bool> condition, string format, string defaultText) where T : struct {
 if(condition == null) condition = v => v.IsNotEmpty();
 return (condition(value) ? string.Format(format, value) : defaultText);
}
Example:
var dt = DateTime.Now;
Console.WriteLine(dt.FormatIf(d => d.Date != DateTime.Now.Date, "The selected day is {0:d}", "today"));

Description

Conditionally formats any value type based on a Lambda Expression

Details

Double click on the code to select all.

 

;