Null _ NullFormatter - axuno/SmartFormat GitHub Wiki
The NullFormatter
outputs a custom string literal, if a variable is null
, else another literal (default is string.Empty
) or nested Placeholder
.
{ Any Value : isnull : output-if-null | output-not-null }
Value | formatter name | outputs |
---|---|---|
Any value | "isnull" | one of the 2 options |
- output-not-null: A text literal or a nested placeholder.
string Name
: default isisnull
The name to use a named formatter
char SplitChar
: default is'|'
The split character for the 2 output options
Smart.Format("{0:isnull:N/A}", default(object));
// outputs: "N/A"
int? arg = null;
Smart.Format("{0:isnull:N/A|Value is {:000}}", arg);
// outputs depending on arg:
arg == null: "N/A"
arg == 12: "Value is 012"
Nullable Number has member Value == 1:
var arg = new { Number = new { Value = 1} };
Smart.Format("{Number?.Value:isnull:N/A|Value is {:000}}", arg);
// outputs: "Value is 001"
Nullable Number is null
, Value won't get evaluated:
var arg = new { Number = default(object) };
Smart.Format("{Number?.Value:isnull:N/A|Value is {:000}}", arg);
// outputs: "N/A"