C# get and set properties for a List Collection

后端 未结 4 1277
清歌不尽
清歌不尽 2021-01-04 00:30

How are properties for a Collection set?

I\'ve created a class with a Collection properties. I want to add to the List anytime I set a new value. Using _name.Add(val

4条回答
  •  情书的邮戳
    2021-01-04 00:52

    Your setters are strange, which is why you may be seeing a problem.

    First, consider whether you even need these setters - if so, they should take a List, not just a string:

    set
    {
        _subHead = value;
    }
    

    These lines:

    newSec.subHead.Add("test string");
    

    Are calling the getter and then call Add on the returned List - the setter is not invoked.

提交回复
热议问题