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

  • Home
  • SEARCH
  • 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 112905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:37:06+00:00 2026-05-11T02:37:06+00:00

I have a class that defines a read-only property that effectively exposes a private

  • 0

I have a class that defines a read-only property that effectively exposes a private field, something like this:

public class Container {     private List<int> _myList;      public List<int> MyList     {         get { return _myList;}     }      public Container() : base ()     {         _myList = new List<int>();     }      // some method that need to access _myList     public SomeMethod(int x)     {          _myList.Add(x);     } } 

now it’s impossible for the consumer to manage my property directly, so code like aContainer.MyList = new List(); generates a compile-time error. However, the consumer is absolutely free to call all sorts of methods on the reference he got, so this is perfectly valid code

Container c = new Container();   Console.WriteLine(c.MyList.Count);   c.MyList.Add(4);   Console.WriteLine(c.MyList.Count);   

which kind of defeats the whole read-only concept.

Is there any sane workaround that would enable me to have a real read-only reference propery?

P.S. I cannot just return a copy of the list because then the user will think that he made all the changes necessary, but alas… they will be gone.

  • 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. 2026-05-11T02:37:07+00:00Added an answer on May 11, 2026 at 2:37 am

    The reference is ‘readonly’, the the actual object. I.e. you can’t replace the reference with another object. So if you have a class that breaks it like this:

    public class Container {     private readonly  List<int> _myList;      public List<int> MyList     {         get { return _myList;}     }      public Container() : base ()     {         _myList = new List<int>();     }      public void BreakReadOnly()     {         _myList = new List<int>();     } } 

    …then it won’t even compile. It’s because a readonly field can’t be reassigned with any other object. In this case BreakReadOnly will try to assign a new list.

    If you really want a readonly collection of it then you can do it like this:

        public ReadOnlyCollection<int> MyList     {         get { return _myList.AsReadOnly(); }     } 

    Hope this helps.

    Updated: Removed use of IEnumerable.

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

Sidebar

Related Questions

I have following class that defines attached property to set children's margin: public class
I have an actionscript file that defines a class that I would like to
I've got a class that has a read-only property defined that is actually a
Suppose you have class B with lazily loaded property c . And that this
I have a class that defines the names of various session attributes, e.g. class
I have a class that defines a CallRate type. I need to add the
I have a python module that defines a number of classes: class A(object): def
I have a few tables that I've defined like the below examples: class TableA
I'm trying to do something a bit unusual... I have this class Foo :
What I am trying to achieve is something like this: class object: def __init__(self):

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.