Skip to content
| Marketplace
Sign in
Visual Studio>Tools>String.Format Diagnostic (Roslyn)
String.Format Diagnostic (Roslyn)

String.Format Diagnostic (Roslyn)

AdamSpeight2008

|
1,802 clicks
| (0) | Free
Validation of format strings at pre compile time!
Get Started

String Format Diagnostic enables the validation of format strings at compile-time.

Why Do This?

Why? This turns a set of run-time exceptions into compile-time warnings!

Supported Methods

  .ToString() on

  • Int16, Int32, Int64, UInt16, UInt32, UInt64
  • Byte, UByte
  • Double, Single
  • Decimal
  • DateTime, TimeSpan, DateTimeOffset
  • Enum

.ParseExact on

  • DateTime
  • DateTimeOffset

.TryParseExact on

  • DateTime
  • DateTimeOffset

Note: The method overload which you can supply an array of format strings are not supported,

C#Visual Basic
Edit|Remove
csharpvb
System.Diagnostics.Debug.WriteLineSystem.Diagnostics.Trace.TraceErrorSystem.Diagnostics.Trace.TraceInformationSystem.Diagnostics.Trace.TraceWarningSystem.IO.TextWriter.WriteLineSystem.String.FormatSystem.Console,WriteSystem.Console.WritelineSystem.Text.StringBuilder.AppendFormatString.Format("{0}{2}", 0, 1);Console.Write("{0}{2}", 0, 1);Console.WriteLine("{0}{2}", 0, 1);
String.Format("{0}{2}", 0, 1)Console.Write("{0}{2}", 0, 1)Console.WriteLine("{0}{2}", 0, 1)
System.Diagnostics.Debug.WriteLine System.Diagnostics.Trace.TraceError System.Diagnostics.Trace.TraceInformation System.Diagnostics.Trace.TraceWarning System.IO.TextWriter.WriteLine System.String.Format System.Console,Write System.Console.Writeline System.Text.StringBuilder.AppendFormat  String.Format("{0}{2}", 0, 1); Console.Write("{0}{2}", 0, 1); Console.WriteLine("{0}{2}", 0, 1);  
 

Example of Supported Validation Rules

Currently it supports the following validation checks

  • Is the argument index valid
  • Unexpected Characters
  • Unexpexted Reached End Of Text
  • Argument Index Value is less than the inbuilt limit of 1000000 (.net framework)
  • Alignment Width Value is less that the inbuild limit of 1000000 (.net framework)

Also gives information that the formatstring contains no argument index references. Eg {1}

 Format Argument of an hole is now validated against the type of the refered to object.

The formatstring argument can be:-
  • A String Literal "Hello {0}"
  • A Const variable 

Diagnostic available for both VB.net and C#.     

 

VS2014 (ctp4)

Note: These are beta releases, so bug report are very welcome.

If you discover a format string thet is incorrect validated please file a bug report on the project site, as the validation rules applied are my intereptation of the available documentation.

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft