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

前端 未结 7 1286
执念已碎
执念已碎 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:19

    For the following statement shared by Alex Booker in their answer

    When the compiler encounters an expression-bodied property member, it essentially converts it to a getter like this:

    Please see the following screenshot, it shows how this statement (using SharpLab link)

    public string APIBasePath => Configuration.ToolsAPIBasePath;
    

    converts to

    public string APIBasePath
    {
        get
        {
            return Configuration.ToolsAPIBasePath;
        }
    }
    

    Screenshot:

提交回复
热议问题