Skip to content

Null _ NullFormatter

Bidimus edited this page Nov 4, 2022 · 3 revisions

The NullFormatter outputs a custom string literal, if a variable is null, else another literal (default is string.Empty) or nested Placeholder.

Syntax Details

{ 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.

Configuration:

string Name: default is isnull
The name to use a named formatter

char SplitChar: default is '|'
The split character for the 2 output options

Examples

Only output if null

Smart.Format("{0:isnull:N/A}", default(object));
// outputs: "N/A"

Output null option or variable

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"

Using Null Notation for the variable

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"
Clone this wiki locally