Skip to content
axunonb edited this page Mar 4, 2022 · 21 revisions

string.Format Compatibility

The section string.Format Compatibility is a "must read".

Smart.Format vs. SmartFormatter.Format

Most of the examples in this Wiki are using "Smart.Format(...)". This is for a good reason: Smart.Format(..) automatically initializes the SmartFormatter with default extensions. If you're using SmartFormatter.Format(...) directly, then it's your job to initialize the SmartFormatter before. So for the beginning just leave SmartFormatter.Format(...) alone.

Note: Smart.Format(...) is just the short version for Smart.Default.Format(...).

Error Handling

By default, SmartFormat sets ParseErrorAction.ThrowError for the Parser and FormatErrorAction.ThrowError for the SmartFormatter.

If you change it to Ignore, this can lead to confusing results. It's highly recommended, to turn exceptions on at least while developing and debugging the code:

Smart.Default.Settings.FormatErrorAction = FormatErrorAction.ThrowError;
Smart.Default.Settings.ParseErrorAction = ParseErrorAction.ThrowError;

Formatting Numbers, DateTime, TimeSpan, Currency etc. Culture-aware

Same as with string.Format it may be necessary to supply the CultureInfo in order to generate properly formatted output strings. Example:

Smart.Format(new CultureInfo("en-US"), "{0:C}", 1234) 
// Result: "$1,234.00"

Not sure how to use the TemplateFormatter?

Please read docs for templates.

Parameterless methods

Only parameterless instance methods of a type can be used in a placeholder, extension methods cannot.

Clone this wiki locally