Why does Property Set throw StackOverflow exception?

后端 未结 2 804
小蘑菇
小蘑菇 2020-11-22 03:27

I know java and would normally put in getter/setter methods. I am interested in doing it in C# with the following code, but it throws a StackOverflow exception. What am I do

2条回答
  •  佛祖请我去吃肉
    2020-11-22 03:33

    You are setting the property name inside your property--not the field name. This would work better:

    private string m_firstName;
    
    public String firstName;
    {
        get
        {
            return m_firstName;
        }
        set
        {
            m_firstName = value;
        }
    }
    

提交回复
热议问题