ShYCalculator 0.9.3
dotnet add package ShYCalculator --version 0.9.3
NuGet\Install-Package ShYCalculator -Version 0.9.3
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.
<PackageReference Include="ShYCalculator" Version="0.9.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ShYCalculator" Version="0.9.3" />
<PackageReference Include="ShYCalculator" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ShYCalculator --version 0.9.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ShYCalculator, 0.9.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ShYCalculator@0.9.3
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ShYCalculator&version=0.9.3
#tool nuget:?package=ShYCalculator&version=0.9.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ShYCalculator
ShYCalculator is a high-performance, thread-safe, and extensible mathematical expression evaluator for .NET, based on the Shunting-Yard algorithm.
Features
- ๐ High Performance: Optimized for low allocation (Zero-Allocation paths where possible).
- ๐งต Thread Safe: Stateless execution model designed for high-concurrency environments (Web APIs, Services).
- ๐งฉ Extensible: Add custom functions, operators, and constants.
- ๐๏ธ Builder Pattern: Fluent API for easy configuration.
- ๐ก๏ธ Safe: No
eval()or dynamic compilation risks; strictly parsed. - ๐ Compiled Mode: Parse once, execute many times for maximum performance.
- ๐ Date & Time: Robust support for date parsing, culture-specific formats, and time adjustments.
- ๐ณ Deep Nesting: Supports complex nested expressions (ifs, ternaries, functions).
Quick Start
Installation
dotnet add package ShYCalculator
Basic Usage (Stateless / Thread-Safe)
Recommended for Web APIs or shared services.
var calculator = new ShYCalculatorBuilder()
.WithAllExtensions()
.Build();
var context = new Dictionary<string, double> {
{ "x", 10 },
{ "y", 20 }
};
var result = calculator.Calculate("x * y + sqrt(x)", context);
Console.WriteLine($"Result: {result.Value.Nvalue}"); // Output: 203.16...
Advanced Usage (Custom Functions)
var calculator = new ShYCalculatorBuilder()
.WithMathematics()
.WithConstant("TaxRate", 0.2)
.Build();
var result = calculator.Calculate("100 * (1 + TaxRate)");
Dependency Injection
// Program.cs
builder.Services.AddShYCalculator(options => {
options.WithAllExtensions();
});
// MyService.cs
public class MyService(ShYCalculator calculator) {
public double CalculatePrice(double basePrice) {
var context = new Dictionary<string, double> { { "basePrice", basePrice } };
return calculator.Calculate("basePrice * 1.2", context).Value.Nvalue.Value;
}
}
Compiled Mode (High Performance)
For scenarios where the formula is constant but variables change, use Compile to avoid reparsing.
var compiled = ShYCalculator.Compile("score * multiplier");
if (compiled.Success) {
var runner = compiled.Value;
var result = runner.Calculate(new Dictionary<string, double> { { "score", 100 }, { "multiplier", 1.5 } });
}
AST & Expression Validation
Retrieve the parsed AST for analysis or validate syntax without execution.
// AST
var result = calculator.Calculate("1 + 2 * 3", includeAst: true);
var nodeType = result.Ast?.Type; // "binary"
// Validation
var validation = ShYCalculator.Compile("1 + (2");
if (!validation.Success) Console.WriteLine(validation.Errors[0].Message);
License
Distributed under the MIT License. See LICENSE for more information.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.