Skip to content

NTDLS.ExpressionParser Usage

A fast mathematics parsing engine for .NET with custom variables and functions.

Published Jul 25, 2026

NTDLS.ExpressionParser

What it is. A mathematics parsing engine for .NET that evaluates expression strings - integer, floating point, logic and bitwise operations, nested parentheses, custom variables and custom functions. It compiles each expression to a compact program in one pass (no code generation, no JIT cost), so evaluating is allocation-free at roughly 44 ns per call, and parsing a brand-new expression takes about half a microsecond.

When to reach for it. You need user-supplied formulas - calculator apps, rule engines, pricing expressions, spreadsheet-like columns - evaluated in a hot loop or at high volume. Skip it if you only evaluate a handful of static expressions; double arithmetic written directly in C# is simpler.

Getting started

The quickest path is the static Evaluate method:

var result = Expression.Evaluate("10 * ((1000 / 5 + (10 * 11)))");
Console.WriteLine($"{result:n2}"); // 3,100.00

Pass an output parameter to get a step-by-step explanation of the work:

var result = Expression.Evaluate("10 * ((1000 / 5 + (10 * 11)))", out var explanation);

Console.WriteLine($"{result:n2}");
Console.WriteLine(explanation);
// {
//     1000/5 = 200
//     10*11 = 110
//     200+110 = 310
//     10*310 = 3100
// } = 3100

For repeated evaluation, construct an Expression once and change its parameters - this is the fastest usage pattern:

var expression = new Expression("price * qty * (1 - discount)");

foreach (var order in orders)
{
    expression.SetParameter("price", order.Price);
    expression.SetParameter("qty", order.Quantity);
    expression.SetParameter("discount", order.Discount);

    Console.WriteLine(expression.Evaluate());
}

Custom functions plug in with AddFunction and receive their arguments as a double[]. Names are case-insensitive, and built-in functions like ceil, sum, sqrt, pow, clamp and if are available out of the box:

var expression = new Expression("Ceil(SUM(11.6, 12.5)) + DoStuff(11, 55)");

expression.AddFunction("DoStuff", (double[] parameters) =>
{
    double sum = 0;
    foreach (var p in parameters)
    {
        sum += p;
    }
    return sum;
});

Console.WriteLine(expression.Evaluate()); // 25 + 66

NuGet: NTDLS.ExpressionParser

An unhandled error has occurred. Reload 🗙