NCalc is a fast, lightweight expression evaluator for .NET. It parses and evaluates mathematical and logical expressions with literals, operators, parameters, built-in functions, and custom functions.
NCalc targets modern .NET, .NET Standard 2.0, and .NET Framework 4.8.
Start with the NCalc docs for the full language reference, runtime options, plugins, and API reference.
Useful entry points:
Use NCalc.Core for the expression engine:
dotnet add package NCalc.CoreImportant
NCalcSync and NCalcAsync are obsolete redirect packages. Use NCalc.Core directly.
Add optional packages only when you need them:
dotnet add package NCalc.LambdaCompilation
dotnet add package NCalc.DependencyInjectionusing NCalc;
var result = new Expression("2 + 3 * 5").Evaluate();
// 17var expression = new Expression("Round(Pow([x], 2) + [offset], 2)");
expression.Parameters["x"] = 3.14;
expression.Parameters["offset"] = 10;
var result = expression.Evaluate();
// 19.86var expression = new Expression("SecretOperation(3, 6)");
expression.Functions["SecretOperation"] = args =>
(int)args.Evaluate(0) + (int)args.Evaluate(1);
var result = expression.Evaluate();
// 9Install NCalc.LambdaCompilation first.
using NCalc.LambdaCompilation;
var expression = new Expression("1 + 2");
Func<int> function = expression.ToLambda<int>();
var result = function();
// 3For data types, operators, built-in functions, async evaluation, caching, expression serialization, and advanced configuration, check the docs.
Open an issue for bugs or usage problems. Include the expression, the expected result, the actual result, and your NCalc version.
| Project | Description |
|---|---|
| FastExpressionCompiler | Fast compiler for C# expression trees. NCalc uses it for lambda compilation. |
| Jint | JavaScript interpreter for .NET by Sebastien Ros, the original author of NCalc. |
| JJMasterData | Runtime form generator that uses NCalc for field visibility and other dynamic behavior. |
| NCalc101 | Web application for trying NCalc expressions, developed by Panoramic Data. |
| NCalcJS | TypeScript/JavaScript port of NCalc. |
| PanoramicData.NCalcExtensions | Extension functions for NCalc, including string functions, switch, if, in, typeOf, and cast. |
| Parlot | Fast and lightweight parser creation tools by Sebastien Ros. NCalc uses Parlot in its default parser. |
Using NCalc? Create a PR and add your project here!
NCalc uses Nerdbank.GitVersioning. Each library build can be traced back to the original git commit. Read more in the release documentation.