Expression-Bodied Methods - Writing Concise Methods

Vaibhav • September 9, 2025

Expression-bodied methods in C# allow you to write methods in a concise, single-line form. They are particularly useful for methods that perform a simple calculation or return a value directly. Instead of using curly braces and multiple lines, you can use the => operator to express the method in one line.

Expression-bodied methods were introduced in C# 6.0 for properties and in C# 7.0 for regular methods. They provide a shorthand syntax without changing functionality.

Basic Syntax

The general syntax of an expression-bodied method is:

returnType MethodName(parameters) => expression;

Explanation:

  • returnType specifies the type of value the method returns.
  • MethodName is the method's identifier.
  • parameters are the inputs to the method (can be zero or more).
  • expression is a single expression whose value is returned (for non-void methods) or executed (for void methods).

Example - Simple Addition

int Add(int a, int b) => a + b;

Console.WriteLine(Add(3, 5)); // Output: 8

Explanation: Instead of writing a full method with curly braces:

int Add(int a, int b)
{
    return a + b;
}

The expression-bodied form reduces the boilerplate while keeping the behavior identical.

Void Methods

Expression-bodied methods also work with void methods:

void PrintMessage(string message) => Console.WriteLine(message);

PrintMessage("Hello World!"); // Output: Hello World!

Explanation: Here, the expression is executed directly, and no value is returned. This can make small utility methods extremely concise.

Real-World Analogy

Think of an expression-bodied method as a shortcut button on a coffee machine: instead of performing multiple steps manually (grind, brew, pour), you press a single button and the machine executes the task. Similarly, the method executes a single expression in one line.

Expression-Bodied Properties

The same syntax can be used for properties:

public int Age { get; set; }
public string Info => $"Age is {Age}";

Age = 25;
Console.WriteLine(Info); // Output: Age is 25

Explanation: The Info property returns the expression value without needing a separate get block.

Expression-Bodied Indexers

You can also use this concise syntax for indexers:

string this[int index] => $"Index value is {index}";

Console.WriteLine(this[3]); // Output: Index value is 3

Advantages

  • Conciseness: Reduces boilerplate code for simple methods.
  • Readability: Keeps simple logic easy to read in one line.
  • Encourages clean design: Forces developers to write focused, single-expression methods.
  • Works for void and non-void methods, properties, and indexers.

Limitations and Best Practices

  • Use only for simple, single-expression methods. Complex logic with multiple statements is better expressed with full method syntax.
  • Avoid nesting multiple expressions into one line - readability can suffer.
  • Expression-bodied methods do not support multiple statements, loops, or conditionals directly; use a full method for that.
  • For beginners, consider using full methods first, then refactor to expression-bodied forms as comfort grows.

Expression-bodied methods are ideal for getters, setters, small calculations, and utility methods. They keep your code concise while maintaining readability and clarity. For more complex logic, always prefer full method syntax.

Combining With Optional Parameters

Expression-bodied methods can also leverage optional parameters to make calls simpler:

string Greet(string name = "Guest") => $"Hello, {name}!";

Console.WriteLine(Greet()); // Output: Hello, Guest!
Console.WriteLine(Greet("Vaibhav")); // Output: Hello, Vaibhav!

Explanation: By combining optional parameters with concise methods, you can reduce code clutter further.

Summary

Expression-bodied methods in C# provide a concise syntax for writing small, focused methods. They improve readability, reduce boilerplate, and keep code compact. They work with methods returning values, void methods, properties, and indexers. While they are ideal for simple expressions, avoid using them for complex logic - in such cases, a full method body ensures clarity and maintainability.