What is the => assignment in C# in a property signature

前端 未结 7 1130
执念已碎
执念已碎 2020-11-22 00:29

I came across some code that said

public int MaxHealth => 
         Memory[Address].IsValid ? 
         Memory[Address].Read(Offs.Life.MaxHp)          


        
7条回答
  •  走了就别回头了
    2020-11-22 01:11

    One other significant point if you're using C# 6:

    '=>' can be used instead of 'get' and is only for 'get only' methods - it can't be used with a 'set'.

    For C# 7, see the comment from @avenmore below - it can now be used in more places. Here's a good reference - https://csharp.christiannagel.com/2017/01/25/expressionbodiedmembers/

提交回复
热议问题