Ookii.FormatC 2.3.1

.NET Standard 2.0 .NET Framework 4.8
NuGet\Install-Package Ookii.FormatC -Version 2.3.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package Ookii.FormatC --version 2.3.1
<PackageReference Include="Ookii.FormatC" Version="2.3.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ookii.FormatC --version 2.3.1
#r "nuget: Ookii.FormatC, 2.3.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Ookii.FormatC as a Cake Addin
#addin nuget:?package=Ookii.FormatC&version=2.3.1

// Install Ookii.FormatC as a Cake Tool
#tool nuget:?package=Ookii.FormatC&version=2.3.1


FormatC is a class library for Microsoft .Net that provides syntax highlighting for several programming languages. FormatC includes support for C#, Visual Basic, C++, XML, Transact-SQL and PowerShell. FormatC can be extended to support other languages if necessary.

FormatC takes as input source code in any of the supported languages, and produces HTML as output containing the syntax highlighted source. You need to use the provided CSS file for the syntax highlighting to work. CSS is provided for both light and dark styles, and you can customize the the CSS file to suit your own syntax highlighting preferences.

FormatC is not heavily developed, and adding support for additional languages is unlikely at this point. However, it is still maintained because of a few features that are rare in other syntax highlighters, such as highlighting type names in C# code and parser-based PowerShell formatting.

FormatC's syntax highlighting uses regular expressions, and as such isn't incredibly fast. It's intended use is to add highlighting to small code snippets for inclusion on web pages, not to highlight large files.

FormatC is compatible with .Net Framework 4.8 and up, and .Net Standard 2.0.

Formatting source code is easy. Here is an example of how to format C# code:

var formatter = new CodeFormatter()
    formatter.FormattingInfo = new CSharpFormattingInfo()

var code = File.ReadAllText("code.cs");
var formattedCode = formatter.FormatCode(code);

You can also write directly to a TextWriter.

For more usage information, please refer to the class library documentation.

The generated output will use CSS class names for different code elements. A CSS stylesheet is provided for light and dark styles.

You can view the included sample output to see what the output of FormatC looks like, or format your own code with the online syntax highlighter.

You can also try it on .Net Fiddle.

Contextual keywords

The C# and Visual Basic languages contain several keywords that are contextual keywords. For example, 'from' is a keyword in a Linq expression, but not elsewhere, and you can still have variables named 'from' without prefixing them with @ (in C#) or enclosing them in [] (in Visual Basic).

Because of the limitations of regular expressions, FormatC cannot determine when a contextual keyword should be treated as a keyword and when it should be treated as a regular identifier. Because of this, it always treats them as keywords.

You can prefix an identifier that is also a contextual keyword with ` (e.g. `from) to prevent it from being highlighted as a keyword. The ` character will not appear in the formatted output, and the identifier will not be highlighted.

Type names

You can specify identifiers that should be colored as type names using the Types property of both the CSharpFormattingInfo and VisualBasicFormattingInfo classes. These identifiers will then always be formatted as type names (even in contexts where they are not). Like with contextual keywords, you can prefix an identifier with ` to prevent it from being highlighted as a type name.

For example:

var formatter = new CodeFormatter()
    formatter.FormattingInfo = new CSharpFormattingInfo() 
        Types = new[] { "Console" }

var code = formatter.FormatCode("Console.WriteLine(\"foo\")");

XML Literals in Visual Basic

Visual Basic XML literals are supported, however the XML literals must be marked explicitly with with [xml][/xml]. For example, this would look like this with a simple XML literal:

Dim xmlLiteral = [xml]<Foo />[/xml]

The [xml][/xml] tags will not be included in the output, and the contents of those tags will be formatted as XML literals. Embedded expressions in XML literals (which are delimited by <%= %> blocks) are also supported, and the contents of embedded expressions will be formatted as Visual Basic code.

PowerShell formatting

While most formatters in FormatC use regular expressions, PowerShellFormattingInfo is capable of using the actual PowerShell parser to tokenize the input. To do this, the project that uses FormatC must reference System.Management.Automation (starting with version 2.2, Ookii.FormatC does not reference this anymore by itself). Alternatively, you can manually load the assembly and pass it to the PowerShellFormattingInfo constructor.

If System.Management.Automation is not available, FormatC will fall back to using regular expressions.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.3.1 60 9/14/2022
2.3.0 80 9/14/2022
2.2.0 259 5/6/2021
2.1.0 466 10/8/2019