How to avoid stack overflow errors when defining set accessor in C#

前端 未结 4 908
眼角桃花
眼角桃花 2021-01-18 19:45

People of stackoverflow. I am new to c# and this is the first time I have not been able to find an answer to one of my elementary questions. Who can help me?!I am trying to

4条回答
  •  旧时难觅i
    2021-01-18 20:51

    In this case instead of generating a new variable like the rest of the answers are saying. You can simply just write.

    public string Headline { get; set;}
    

    This will allow anyone to retrieve this variable which you could re write as

    public string Headline;
    

    If you want the setting to be private you can say.

    public string Headline { get; private set; }
    

    I like it this way better because this way you don't have to allocate a new variable and the readability of the code greatly increases :)

提交回复
热议问题