List formatter
Format the items of a collection and join them with spacers, or index into a list with a selector.
Read time 3 minutesLast updated 13 days ago
Format the items of a collection and join them with spacers, or index into a list with a selector.
The List formatter is both a formatter and a source. As a formatter it repeats a format for each item of any IEnumerable, such as an array or a , joining the items with spacers. As a source it provides list-item selectors, so you can read an item by index or by the current iteration index. Apply the formatter explicitly with the name , or omit the name to apply it implicitly to a collection.
List<T>listAnatomy of the list formatter syntax.
The format takes up to four parts, separated by the split character:
Part | Description |
|---|---|
| Item format | The format applied to each item. An empty placeholder |
| Spacer | Written between items. |
| Last spacer | Optional. Replaces the spacer before the final item. |
| Two spacer | Optional. Used as the only spacer when the list has exactly two items. |
The item format can contain nested placeholders, so each item can be formatted in detail, for example .
{Sizes:list:{Width}x{Height}|, }Spacers can include character literals, for example to put each item on its own line. A spacer that contains nested placeholders is formatted against the collection's parent value, not the individual item.
\nFormat a list
Smart String | Argument | Result |
|---|---|---|
| | one, two, and three |
| | one and two |
| | 1.00, 2.00, 3.00 |
List-item selectors
As a source, the List formatter lets a selector index into an IList. Use a numeric selector to read a specific item, and the selector to read the current item's position while iterating.
indexA string is an , but the formatter does not treat strings or values as lists.
IEnumerableIFormattableSmart String | Argument | Result |
|---|---|---|
| | The value at index 1 is 2 |
| | 1 Hello World |
Use the selector to synchronize two lists by iterating one and indexing the other at the same position:
indexSmart String | Argument | Result |
|---|---|---|
| | 1 = one, 2 = two, 3 = three, 4 = four |