How to Write an Expression-Bodied Method with => in C#
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
<modifiers> <ReturnType> <Name>(<parameters>) => <expression>;
| Part | What 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
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

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
dotnet run main.cs
16
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:
Console.WriteLine(new Calculator().Square(4));
class Calculator
{
public int Square(int n) => return n * n;
}
/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
| Mistake | What happens | Do 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 expression | Use a normal body with braces |
| Returning a value of the wrong type | The expression must convert to the return type | Change the expression or the return type |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
CS1525 and CS1002 on a method line | return after => | Remove return |
| The method grew and is hard to read | Too much logic on one line | Turn 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
- Expression body definition, on Microsoft Learn.
