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#
| Setting | What it is |
|---|---|
| Name, Unit | As for any variable. |
| C# code | The body of a function that returns the value. Up to 3,000 characters. Ctrl Enter applies it and runs. |
| The signature | Shown 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,MaxandAbs, 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 ownMathand a bareMathwon’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.
See also
- Write a rule in C#When a formula can't say it: ifs, loops and local variables in a block of your own C#.
- VariableA value worked out from others by a formula, at every step.
- FormulasWhat a formula can contain: names, t, operators and functions.
- Read and run the C# behind a modelThe program SepalSolver runs, and how to take it to the terminal and change it.