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

前端 未结 4 966
眼角桃花
眼角桃花 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条回答
  •  忘掉有多难
    2021-01-18 20:41

    You need a backing field if you are trying to use set and get with your property.

    private string _headline; //backing field.
    
    public string Headline
    {
        get { return _headline; }
        set { _headline = value; }
    } 
    

    In your current code, you are trying to set your property recursively, and thus resulting in stackoverflow exception

提交回复
热议问题