The expression language includes a grammar to provide formatting groups, references and coalescing in C# strings.
Example
Below is a short example that uses these constructs to create a very flexible formatting string for a person.
...
The person's
id
is always includes at the beginning (regardless of whether it is set to a non-null value).If both
ln
andfn
are set, they are both included and separated with a comma and a space.If only one of
ln
orfn
is set, that value is included.If neither
ln
orfn
is set, the word "withheld" is included instead.
Referencing data
References and embedded expressions are indicated with curly brackets:
...
Reserved characters (<
, ?
and {
) can be escaped with a backslash.
More examples
The following examples will try to illustrate the various paths that an evaluation of a formatting group can take.
...
A | B | C | Result |
---|---|---|---|
1 | 2 | 3 | 1, 2 |
2 | 3 | 2 | |
1 | 3 | 1 | |
3 | 3 | ||
Nothing defined! |
Advanced Examples
A real-world example with several data fields can take a few more iterations to get them right. Let's take a look below.
...
As you can see, the fallbacks for missing data are now better, printing "No data" when absolutely nothing has been specified.
Writing tests
The following code illustrates how to test an expression using C# and NUnit with various inputs.
...