📉Text - NKS0001
Use TextTools.FormatString() instead of string.Format()
Last updated
Use TextTools.FormatString() instead of string.Format()
Last updated
This analyzer provides the following strings:
Context | String |
---|---|
This code analyzer detects the usage of the standard string.Format()
method from the regular .NET core libraries, and highlights the relevant parts as yellow squiggly lines indicating a warning.
The reason is that string.Format()
tries to format any text within the .NET string formatting syntax defined in this link, and throws an exception if any of these syntaxes are invalid. However, TextTools.FormatString()
is clever at handling such exceptions and returns the unformatted string in case formatting fails.
Also, TextTools.FormatString()
doesn't attempt to format any string and returns the plain string that's passed to it if there are no arguments to be passed to the formatter, making it impossible to crash if it was used without arguments accidentally or intentionally.
This analyzer detects the usage of string.Format()
and fixes it by replacing the call with TextTools.FormatString()
, importing the KS.Misc.Text
namespace if necessary.
To get a brief insight about how this analyzer works, compare the two code blocks shown to you below:
You can suppress this warning by including it in the appropriate place, whichever is convenient.
For more information about how to suppress any warning issued by the Nitrocid analyzer, visit the below page:
We recommend that every caller which use the string.Format()
method use the recommended abovementioned method.
Error List
Caller uses string.Format()
instead of TextTools.FormatString()
Suggestion Box
Use TextTools.FormatString()
instead of string.Format()
Description
TextTools.FormatString()
uses the error handler to handle unknown formatting errors and returns the unformatted string if such errors happen, but string.Format()
immediately throws.