[c#] C# get and set properties for a List Collection

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(value) within the set method doesn't work.

Section newSec = new Section();

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

public class Section
{
    public String head { get; set; }
    private List<string> _subHead = new List<string>();
    private List<string> _content = new List<string>();

    public List<string> subHead
    {
        get
        { return _subHead; }
        set
        {
            _subHead.Add(value);
        }
    }
    public List<string> content
    {
        get
        { return _content; }
        set
        {
            _content.Add(value);
        }
    }
}

Update with my solution:

public class Section
{

    private List<string> _head = new List<string>();
    private List<string> _subHead = new List<string>();
    private List<string> _content = new List<string>();

    public List<string> Head
    {
        get
        { return _head; }
    }

    public List<string> SubHead
    {
        get
        { return _subHead; }
    }
    public List<string> Content
    {
        get
        { return _content; }
    }

    public void AddHeading(string line)
    {
        Head.Add(line);
    }

    public void AddSubHeading(string line)
    {
        SubHead.Add(line);
    }

    public void AddContent(string line)
    {
        Content.Add(line);
    }

}

This question is related to c#

The answer is


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.


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.


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.