Reference: components

C# block

Updated 3 November 2026 · 1 min read

A C# block is a variable worked out by C# you write: ifs, loops and local variables, ending in a return. Use one for a rule that a single formula can’t express.

Settings#

SettingWhat it is
Name, UnitAs for any variable.
C# codeThe body of a function that returns the value. Up to 3,000 characters. Ctrl Enter applies it and runs.
The signatureShown above the code: the function your code is the body of, taking the time and every value your code reads. Worked out for you.

What the code can use#

  • t, and any stock, constant or variable by its name in formulas.
  • Your lookup tables, as functions.
  • Exp, Log, Pow, Sqrt, Min, Max and Abs, as in formulas.
  • Any C#: local variables, if, loops, switch. Write anything else from C#’s maths library in full, System.Math.Round, because SepalSolver has its own Math and a bare Math won’t compile.

The code must end by returning a number on every path. Braces and comments must close; the panel says when they don’t.

In the C##

The block becomes a function of its own, and the variable calls it:

// Bill, in $
double Block_Bill(double t, double WaterUsed, double LowPrice, double HighPrice)
{
    // the first 100 m³ at the low price, the rest at the high price
    if (WaterUsed <= 100) return WaterUsed * LowPrice;
    return 100 * LowPrice + (WaterUsed - 100) * HighPrice;
}
...
    double Bill = Block_Bill(t, WaterUsed, LowPrice, HighPrice);                // $

Safety#

C# can do anything a program can. A model opened from a link that contains C# blocks won’t run until you choose to run it; read each block’s code first.