Table of Contents

Async Support

NCalc supports asynchronous evaluation through Expression.EvaluateAsync. Unlike popular belief, async is not faster than sync, but scales better. It's recommended to use async if your expression is doing any CPU or IO bound work in a dynamic function or parameter, so your UI does not freeze or your web server can keep handling other work. Learn more about async in this article.

Usage

var expression = new Expression("database_operation('SELECT FOO') == 'FOO'");

expression.AsyncFunctions["database_operation"] = async args =>
{
    // My heavy database work.
    await Task.Delay(100, args.CancellationToken);

    return "FOO";
};

var result = await expression.EvaluateAsync();
Debug.Assert((bool)result!);