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 962645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:32:27+00:00 2026-05-16T01:32:27+00:00

I have a class with a collection member. I would like to prevent external

  • 0

I have a class with a collection member. I would like to prevent external code from modifying this collection directly, instead using methods (which can perform the appropriate validation etc).

This is harder than I thought. Here is the solution I am using. Can you please tell me if there are better ways to do this common thing? It all just seems a little overengineered.

using System.Collections.Generic;
using System.Collections.ObjectModel;
public class Foo
  {
    private List<Bar> _bars = new List<Bar>();

    public ReadOnlyCollection<Bar> Bars { get { return _bars.AsReadOnly(); } } 

    public void AddBar(Bar bar) //black sheep
    {
      //Insert validation logic here
      _bars.Add(bar);
    }
  }
  • 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-16T01:32:28+00:00Added an answer on May 16, 2026 at 1:32 am

    I think it’s a good solution. This approach is discussed by Martin Fowler here Incapculate collection

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Model called Version that looks like this: from google.appengine.ext import db
I have a class, say DerivedBindingList<T> , which is derived from BindingList<T> . I
I have a concrete class that contains a collection of another concrete class. I
Is it possible to have a static collection as a member of a hibernate
I have a doubt concerning declaring variables, their scope, and if their address could
I'm looking for a good guide into the right method of using Linq to
I am currently converting what was a Console Application into a Windows Form application.
Couldn't seem to think of a good title, my apologies. Feel free to change
Possible Duplicates: ‘Friends’ equivalent for Java? Is there a way to simulate the C++

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.