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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:41:46+00:00 2026-06-06T06:41:46+00:00

Situation: I often run into this problematic and never know how to solve it.

  • 0

Situation:

I often run into this problematic and never know how to solve it. Though I don’t know where and how to look for answer.
So here it is. When you store some objects into other objects you can have them as readonly, or just with a getter, which is the same. This way you cannot change the value of a MyInt in

public class ClassA {

    public readonly int MyInt;

    public string MyString;

}

and no problem if I want to put this in a container as follows:

public class ClassB {

    public readonly ClassA MyClassA;

    public string MyString;

}

This will still work as expected. MyInt is readonly and MyStringA is not.

Problem:

But I can still get MyClassA and set MyClassA.MyStringA.
Is there a way to have a “stricter” readonly system?
With the above example I would like MyClassA’s fields to be all readonly if MyClassA is.

The only solution I can think of is to have another class MyClassAreadonly. But that seems ugly and not not convenient.

Context:

The reason why I am looking for this behavior is that I want MyClassB.MyString to add logic (like firing an event) before setting MyClassB.MyClassA.MyString. So another solution is to simply not show ClassB.MyClassA by setting it as private. But that is nice to being able to retrieve MyClassA! But if I retrieve it and modify it I miss the ClassB logic!

PS: Despite I can’t figure it out, I hope to have been clear enough.

  • 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-06-06T06:41:48+00:00Added an answer on June 6, 2026 at 6:41 am

    “Deep” immutability

    As stated in a comment by @Damien_The_Unbeliever, here is a perfect “answer” to my question. I put answer between quote marks because there is no built-in solution for the moment in C#.

    The closest alternative to address my issue will be to have an Immutable facade for the objects (MyClassA) I want to use this way.

    Thanks Damien.

    Another read on the subject: http://www.bluebytesoftware.com/blog/2007/11/11/ImmutableTypesForC.aspx

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

Sidebar

Related Questions

I often run into the situation where I want to disable some code while
I often run into a situation where I need to come up with a
I often run into a situation where I want to subscribe to an event,
I run into this problem again and again; Situation there is a (large) dataset
All too often I've run into a situation in which a view in my
This is often situation, but here is latest example: Companies have various contact data
I so often run into this pattern. Can someone please give me a canonical
Here's a situation that comes up quite often that never sits too easy with
I often run into a situation where I want to have a set of
I often run into a situation where I want to print a 2d array

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.