Summary#
Inside render { … } a value can be given a name:
var lapsed = DateTime.UtcNow > deadline; // inferred from the initializer
bool lapsed = DateTime.UtcNow > deadline; // declared, and checkedBoth are the same binding. The name is visible to the following siblings in the block — not to anything before it, and not outside it — which is the scope a C# local has.
Signature#
var <name> = <expression>; // the type is inferred from the initializer
<Type> <name> = <expression>; // the type is declared, and the initializer is checked against itDescription#
Why name a value at all#
A render expression is read where it is written, so a value used twice is otherwise written twice. Naming it once is shorter to read and impossible to get subtly different in the second copy.
Inferred or declared#
var takes the initializer's type. A declared type pins the binding, which is not always the same thing:
var half = 1; // int — `half / 2` is 0
decimal half = 1; // decimal — `half / 2` is 0.5The declared type is checked with the same rule a method body uses: the initializer must be assignable to it. A
derived value goes into a base-typed binding, a literal takes the C# constant conversion, and there is no implicit
conversion between decimal and double in either direction. An initializer that does not fit is a compile error
naming both types.
A binding must be initialized where it is declared — there is no definite-assignment analysis, so bool flag;
on its own is refused.
What a binding cannot do#
A binding names a value; it does not introduce a data read. Its initializer is held to the same client-runnable
rule as every other render expression, so it cannot reach the server, write state, or run an effect. A value that
needs a query belongs in a live var component field.
Examples#
A status label and a flag, each computed once and read several times:
[Page("/requests")]
[Render(CSR)]
[AllowAnonymous]
component Requests() {
int[] ages = [1, 2, 5];
render {
Stack {
// Declared, because the type is the point of the value.
string heading = "Requests";
Text(heading);
foreach (var age in ages) {
// Read twice below — written once here.
bool lapsed = age > 3;
Text($"{age}d: {(lapsed ? "lapsed" : "open")}{(lapsed ? " (closed)" : "")}");
}
}
}
}See also#
- component — component fields, including
live varfor values that read data - Calling helpers from render — calling a pure helper from a render expression