Back to Blog
C#

C# Expression-Bodied Method: Syntax and Usage

c# expression bodied method: Learn how C# expression-bodied methods simplify single-expression logic, where the syntax applies, and when a block body is the clearer ch...

C#Expression-Bodied MembersMethod SyntaxCode Readability.NET
A compact arrow symbol transforming a multi-line method block into a single-line expression, illustrating the C# expression-bodied method syntax.

c# expression bodied method requires a clear understanding of the core syntax, runtime behavior, and practical implementation patterns demonstrated in the examples below.

The C# expression-bodied method is a compact syntax that replaces a method's block body with a single expression using the => operator. Instead of braces and an explicit return, you write the expression directly after the arrow:

// Block body public int Add(int a, int b) { return a + b; } // Expression body public int Add(int a, int b) => a + b;

Both forms compile to identical IL. The expression-bodied syntax is syntactic sugar — it changes neither runtime behavior nor performance. The compiler emits the same method either way.

Expression-bodied methods were introduced in C# 6. C# 7 extended the syntax to properties, indexers, constructors, finalizers, and operators. The rules for what counts as a valid expression are the same across all of these members.

How the Syntax Works

An expression-bodied method requires exactly one expression. When the method declares a return type, the expression's value becomes the return value. The compiler applies the same implicit conversion rules as a return statement in a block body.

public decimal GetTotal() => _price * _quantity; public bool IsValid(string input) => !string.IsNullOrWhiteSpace(input);

For void methods, the expression must be a statement that produces no value, such as a method call or an assignment:

public void Log(string message) => Console.WriteLine(message); public void Reset() => _counter = 0;

An expression body cannot contain local variable declarations, if statements, switch statements, loops, or try blocks. Control flow that requires those constructs must use a block body.

Expression-Bodied Properties and Indexers

A read-only property can use an expression body for its getter:

public string FullName => $"{FirstName} {LastName}";

This is equivalent to:

public string FullName { get { return $"{FirstName} {LastName}"; } }

Indexers follow the same pattern:

public string this[int index] => _items[index];

The syntax also works for operators and constructors. A constructor expression body can assign fields or call another constructor:

public Point(int x, int y) => (X, Y) = (x, y);

When Expression Bodies Improve Readability

The value of the syntax is concision for methods that are genuinely a single expression. Small accessors, simple computations, and delegation to another method are the natural fit:

public bool IsActive => _status == Status.Active; public decimal Total => _price * _quantity; public void Notify() => _mediator.Send(new OrderCreated(_id));

The shorter form removes two lines of braces and a return keyword, which makes a class with many small members easier to scan. When a method contains multiple statements, a loop, or conditional logic beyond a ternary, an expression body is the wrong tool. Forcing that logic into one expression makes the code harder to read than the block form it replaces.

Common Mistakes and Limitations

A common mistake is reaching for an expression body when the logic requires statements. You cannot use if, switch, foreach, or try inside an expression body. A ternary or a switch expression can cover some conditional cases, but anything more complex belongs in a block body.

Another limitation: expression-bodied methods cannot contain yield return. Iterator methods must use a block body:

// This does not compile public IEnumerable<int> GetNumbers() => yield return 1;

The compiler reports a clear error, but the fix is to switch to a block body with yield return inside.

Expression bodies also cannot contain await unless the method is declared async. An async expression-bodied method is valid, but it is rarely clearer than the block form:

public async Task<string> FetchAsync() => await _client.GetStringAsync(url);

Performance and Maintainability Considerations

There is no performance difference between an expression-bodied method and a block-bodied method. The compiler emits the same IL for both. Any claim that expression bodies are faster is incorrect — the choice is purely about source readability.

From a maintainability perspective, expression bodies reduce visual noise for simple methods. But they can obscure intent when the expression is long. A method like this is harder to read than its block-bodied equivalent:

public bool CanShip() => _status == Status.Ready && _address != null && _weight > 0 && _carrier != null;

When an expression spans several lines or requires careful parsing, an explicit return with a block body is usually clearer. The goal is to make the method's behavior obvious at a glance, not to minimize line count.

Deciding Between Expression and Block Bodies

Use an expression body when the method is a single expression that is short enough to read at a glance and whose purpose is immediately clear. Use a block body when the method contains multiple statements, involves control flow, or would require an expression too long to parse quickly.

There is no rule that every single-expression method must use an expression body. Consistency within a codebase matters more than applying the syntax everywhere possible. If a team standardizes on block bodies for all methods, that is a defensible choice. If expression bodies are used for simple accessors and computations, that is equally defensible. The syntax exists to make code clearer, not to satisfy a style checklist.

c# expression bodied method: Practical Usage and Code Exampl | RYUSLOG DEV