=>
is used for lambda-functions as you mentioned. It's also used for expression-bodied members.
The documentation on expression-bodied members is available here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members
- C# 6.0 in 2015 added support for expression-bodied methods.
C# 7.0 in 2016 added support for expression-bodied properties and other members.
In methods and property get
blocks, it's equivalent to { return X; }
.
- In properties:
- If the property has a single expression-body, then it's equivalent to declaring a getter-only
get { return X; }
property.
- If the proeprty has an explicit
get
, then it's equivalent to ``
So this:
public int Health
{
get => _health;
set { _health = value; CaluateHeath(); }
}
Is equivalent to:
public int Health
{
get { return _health };
set { _health = value; CaluateHeath(); }
}
And this:
public override double TotalSpecialPower => 1000;
Is equivalent to:
public override double TotalSpecialPower
{
get { return 1000; }
}
And this (note the parentheses):
public override double TotalSpecialPower() => 1000;
Is equivalent to:
public override double TotalSpecialPower()
{
return 1000;
}