C# get and set properties for a List Collection

后端 未结 4 1234
清歌不尽
清歌不尽 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<string>, 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<string> - the setter is not invoked.

    0 讨论(0)
  • 2021-01-04 00:54

    It would be inappropriate for it to be part of the setter - it's not like you're really setting the whole list of strings - you're just trying to add one.

    There are a few options:

    • Put AddSubheading and AddContent methods in your class, and only expose read-only versions of the lists
    • Expose the mutable lists just with getters, and let callers add to them
    • Give up all hope of encapsulation, and just make them read/write properties

    In the second case, your code can be just:

    public class Section
    {
        public String Head { get; set; }
        private readonly List<string> _subHead = new List<string>();
        private readonly List<string> _content = new List<string>();
    
        // Note: fix to case to conform with .NET naming conventions
        public IList<string> SubHead { get { return _subHead; } }
        public IList<string> Content { get { return _content; } }
    }
    

    This is reasonably pragmatic code, although it does mean that callers can mutate your collections any way they want, which might not be ideal. The first approach keeps the most control (only your code ever sees the mutable list) but may not be as convenient for callers.

    Making the setter of a collection type actually just add a single element to an existing collection is neither feasible nor would it be pleasant, so I'd advise you to just give up on that idea.

    0 讨论(0)
  • 2021-01-04 01:01

    Or

    public class Section
    {
      public String Head { get; set; }
      private readonly List<string> _subHead = new List<string>();
      private readonly List<string> _content = new List<string>();
    
      public IEnumerable<string> SubHead { get { return _subHead; } }
      public IEnumerable<string> Content { get { return _content; } }
    
      public void AddContent(String argValue)
      {
        _content.Add(argValue);
      }
    
      public void AddSubHeader(String argValue)
      {
        _subHead.Add(argValue);
      }
    }
    

    All depends on how much of the implementaton of content and subhead you want to hide.

    0 讨论(0)
  • 2021-01-04 01:06

    If I understand your request correctly, you have to do the following:

    public class Section 
    { 
        public String Head
        {
            get
            {
                return SubHead.LastOrDefault();
            }
            set
            {
                SubHead.Add(value);
            }
    
        public List<string> SubHead { get; private set; }
        public List<string> Content { get; private set; }
    } 
    

    You use it like this:

    var section = new Section();
    section.Head = "Test string";
    

    Now "Test string" is added to the subHeads collection and will be available through the getter:

    var last = section.Head; // last will be "Test string"
    

    Hope I understood you correctly.

    0 讨论(0)
提交回复
热议问题