Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6828055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:20:18+00:00 2026-05-26T22:20:18+00:00

How are properties for a Collection set? I’ve created a class with a Collection

  • 0

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);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:20:19+00:00Added an answer on May 26, 2026 at 10:20 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a collection of a class' properties and would like to update each one's
I want to bind my UI against a collection of XElements and their properties
Binding a List collection to a datagrid. How can you limit what properties will
I'm using garbage collection in Objective-C 2.0. Do I need to retain properties? Eg.
Do properties on an object within an Observable collection require a PropertyChangedEvent? For example:
I have a class having 2 properties (both integers) and storing class into List
I'm using a loop to fill a collection. There are a few properties for
I have a need to set a collection of name/value pairs inside my application
I have set one of my properties in my base class to have a
I am using a set of DataGridViews (dgv) to display a class's members via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.