Skip to main content

How to Write an Expression-Bodied Method with => in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Many methods only work out one value and return it. In C#, such a method can drop its braces and its return: write => and the expression, as in public int Square(int n) => n * n;.

Syntax​

Syntax
<modifiers> <ReturnType> <Name>(<parameters>) => <expression>;
PartWhat it does
<modifiers> <ReturnType> <Name>(<parameters>)The method's usual declaration, such as public static string Describe(int n).
=>Separates the declaration from its body.
<expression>;The one expression the method returns. Its result must convert to <ReturnType>.

It's shorthand for a method whose body is { return <expression>; }. Microsoft's => operator reference: "The => token is supported in two forms: as the lambda operator and as a separator of a member name and the member implementation in an expression body definition." And: "For a member that returns a value, the expression's result must be implicitly convertible to the member's return type."

The Example​

A small Calculator class has two one-line methods: Square, which multiplies a number by itself, and a static method, Describe, which says whether a number is even or odd.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which prints calculator.Square(4) and Calculator.Describe(7), where the Calculator class declares Square as n times n and Describe as even or odd, both with the => arrow and no braces.

main.cs
var calculator = new Calculator();
Console.WriteLine(calculator.Square(4));
Console.WriteLine(Calculator.Describe(7));

class Calculator
{
public int Square(int n) => n * n;

public static string Describe(int n) => n % 2 == 0 ? "even" : "odd";
}

Square is called on a Calculator object; Describe is static, so it's called on the class itself. n % 2 == 0 is true when dividing by 2 leaves nothing over, which means the number is even.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
16
odd

A zsh terminal where dotnet run main.cs prints 16 and odd.

Square(4) returned 16, and 7 leaves 1 over when divided by 2, so Describe(7) returned odd. Both methods behave exactly like versions with braces and return. The command is the same in zsh, bash and PowerShell.

What Happens with return After the Arrow​

The expression after => is already the returned value, so return doesn't belong there:

main.cs
Console.WriteLine(new Calculator().Square(4));

class Calculator
{
public int Square(int n) => return n * n;
}
Output
/Users/you/demo/main.cs(5,33): error CS1525: Invalid expression term 'return'
/Users/you/demo/main.cs(5,33): error CS1002: ; expected
/Users/you/demo/main.cs(5,33): error CS1519: Invalid token 'return' in a member declaration

The build failed. Fix the build errors and run again.

All three errors point at the same spot, column 33, where return starts. Delete it.

Common Mistakes​

MistakeWhat happensDo this instead
Writing return after =>error CS1525: Invalid expression term 'return'Write only the expression
Trying to fit several statements after =>An expression body holds one expressionUse a normal body with braces
Returning a value of the wrong typeThe expression must convert to the return typeChange the expression or the return type

Troubleshooting​

SymptomLikely causeFix
CS1525 and CS1002 on a method linereturn after =>Remove return
The method grew and is hard to readToo much logic on one lineTurn it back into a method with braces

FAQ​

Which members can use =>?​

More than methods. The reference lists methods and local functions, operators, read-only properties and indexers, get, set and init accessors, constructors, finalizers and event accessors. A read-only property looks like public int Total => price * quantity;.

Can a void method use =>?​

Yes, when its body is one statement such as a method call: void Greet() => Console.WriteLine("Hi");. For members like that, the reference says the body "must be a statement expression".

Conclusion​

When a method only returns one expression, write => <expression>; instead of { return <expression>; }. Leave out return, keep it to one expression, and switch back to braces once a method needs more than one line.

Additional Resources​