liveBook · Manning

Expression-bodied member

As explained in .NET in Action, Second Edition

An expression-bodied member is a C# member whose implementation is written as a single expression following an expression-body operator.

An expression-bodied member is a C# member implemented by one expression after the expression-body operator. Because the member contains only an expression, it needs neither braces nor a return statement. This shorthand is commonly used for concise properties. section 3.2.2 section 2.8

The expression runs when the property is accessed, not when the containing page model is constructed. Code that initializes dependent state must therefore run before the property is read. section 3.2.2

Computed visibility property

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); public void OnGet() { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; } ShowRequestId derives its value from RequestId. The GET handler assigns RequestId before the Razor page evaluates ShowRequestId.

Conditional rendering in a form page

public bool ShowRenderedText => !string.IsNullOrEmpty(RenderedText); public void OnPost(string text) { RenderedText = FiggleFonts.Standard.Render( text ?? "Hello!"); } The Razor page reads ShowRenderedText to decide whether to display the rendered output.

How .NET in Action, Second Edition covers it

How deep it goes
Defines the shorthand and explains access-time evaluation, without developing broader property permutations or compilation internals.
Examples
Demonstrates ShowRequestId and ShowRenderedText as computed properties controlling Razor page output.
What it assumes
Assumes familiarity with C# properties, null operators, and request handlers while reinforcing concise property syntax.
What you can do after
Apply it to write concise computed properties whose expressions evaluate when callers access them.
How it connects
Connects expression-bodied members with C# properties, nullable reference types, Razor Pages, and handler methods.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.