What Does => (Lambda Operator) Mean in C#?
If you’ve used C++ before, the => operator in C# might feel unfamiliar and confusing at first. However, this operator is used in many different ways in C#, and it plays a key role in writing cleaner and more readable code.
In C#, the => symbol is called the lambda operator. It's most commonly used in lambda expressions, which are a concise way to write small, inline functions.
In this post, we’ll walk through how the => operator works and where it's commonly used, with simple examples.
Common Uses of the => Operator
The => operator appears often in lambda expressions and delegate declarations.
Func<int, int> square = x => x * x;
Action greet = () => Console.WriteLine("Hello!");
The first line defines a lambda that takes x and returns x * x.
The second line defines an Action that prints "Hello!" with no parameters.
Comparing to Traditional Code
The => operator can also be used in method definitions. Here’s a comparison between the regular syntax and the simplified one using =>.
// Traditional method
class MathUtil
{
public int Square(int x)
{
return x * x;
}
}
// Expression-bodied method
class MathUtil
{
public int Square(int x) => x * x;
}
For simple logic, using => makes the code much more concise.
What the => Operator Really Means
The => operator is essentially a shortcut for returning a value without using return and braces { }.
public int Add(int x, int y)
{
return x + y;
}
can be written like this:
public int Add(int x, int y) => x + y;
It's a clean way to write short methods.
Using => in Property Accessors
The => operator can also be used with property get and set accessors, especially when the logic is simple.
public override string Text
{
get => base.Text;
set => base.Text = value;
}
This code means:
- The
getaccessor returnsbase.Text. - The
setaccessor assigns the value tobase.Text.
This reduces unnecessary code and improves readability.
You might also come across code like this:
public string Name => _name;
It may look like the get keyword is missing, but it’s not.
This is actually an expression-bodied read-only property, and C# internally treats it as:
public string Name
{
get { return _name; }
}
So get is not really omitted—it’s just implicitly handled by the compiler.
Be Careful With This
// This will cause a compile error
public string Name => _name;
set => _name = value;
This code results in a syntax error.
Why? Because the first line already declares the entire property using an expression body.
C# sees it as a complete, get-only property and does not allow adding set afterward like that.
The Right Way
If you want to use => with both get and set, you must define both inside a property block:
public string Name
{
get => _name;
set => _name = value;
}
This syntax is supported from C# 7.0 onward.
Summary
- The
=>operator is a shorthand forreturnand{ }. - It’s used in lambdas, methods, and properties.
- A
get-only property can be fully written using=>. - The
setaccessor can use=>too, but only inside a property block. - You cannot use
=>for the entire property and then add asetafterward.
At first, the syntax may feel unfamiliar, but once you get used to it, you’ll be able to write cleaner and more efficient C# code.
