C# Syntax Highlighting

The AnsiConsoleCSharpExtensions class provides extension methods for IAnsiConsole to render C# code with syntax highlighting.

Methods

WriteCSharpAsync (Stream, default styles)

Writes C# code from a stream to the console with default syntax highlighting asynchronously.

Task<string> WriteCSharpAsync(this IAnsiConsole ansiConsole, Stream stream)

Parameters:

Returns: A task representing the asynchronous operation with the parsed string.

WriteCSharpAsync (Stream, custom styles)

Writes C# code from a stream to the console with custom syntax highlighting asynchronously.

Task<string> WriteCSharpAsync(this IAnsiConsole ansiConsole, Stream stream, CSharpStyles csharpStyles)

Parameters:

Returns: A task representing the asynchronous operation with the parsed string.

WriteCSharp (Stream, default styles)

Writes C# code from a stream to the console with default syntax highlighting synchronously.

string WriteCSharp(this IAnsiConsole ansiConsole, Stream stream)

Parameters:

Returns: The parsed string.

WriteCSharp (Stream, custom styles)

Writes C# code from a stream to the console with custom syntax highlighting synchronously.

string WriteCSharp(this IAnsiConsole ansiConsole, Stream stream, CSharpStyles csharpStyles)

Parameters:

Returns: The parsed string.

WriteCSharp (string, default styles)

Writes C# code from a string to the console with default syntax highlighting.

void WriteCSharp(this IAnsiConsole ansiConsole, string value)

Parameters:

WriteCSharp (string, custom styles)

Writes C# code from a string to the console with custom syntax highlighting.

void WriteCSharp(this IAnsiConsole ansiConsole, string value, CSharpStyles csharpStyles)

Parameters:

Example Usage

Basic Usage with String

using Spectre.Console;
using NTokenizers.Extensions.Spectre.Console;

var csharpCode = """
    using System;

    // Main class
    public class Program {
        public static void Main() {
            Console.WriteLine("Hello, World!");
        }
    }
    """;

AnsiConsole.Console.WriteCSharp(csharpCode);

Custom Styles

using Spectre.Console;
using NTokenizers.Extensions.Spectre.Console;
using NTokenizers.Extensions.Spectre.Console.Styles;

var customCSharpStyles = CSharpStyles.Default;
customCSharpStyles.Keyword = new Style(Color.Orange1);

AnsiConsole.Console.WriteCSharp(csharpCode, customCSharpStyles);

Async with Stream

using Spectre.Console;
using NTokenizers.Extensions.Spectre.Console;

await AnsiConsole.Console.WriteCSharpAsync(stream);