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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:45:54+00:00 2026-06-18T19:45:54+00:00

If I have a C# class with only readonly fields, but the fields’ types

  • 0

If I have a C# class with only readonly fields, but the fields’ types are NOT immutable, is the class considered immutable?

Is this class immutable?

public class Foo
{
    private readonly int[] _blah;

    public Foo(int[] blah)
    {
        _blah = blah;
    }

    public int[] Blah { get { return _blah; } }
}

_blah is not immutable, since I can change the members of the array, though the array member variable can never change.

So, is a class immutable if its fields are all readonly, or is a class immutable only if its fields are not only readonly, but also immutable themselves?

  • 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-18T19:45:55+00:00Added an answer on June 18, 2026 at 7:45 pm

    It will really depend on who you ask or what you mean. Some might say that the class itself is immutable because its direct members are immutable. Some might say that the class and all its members (and their properties, etc) — basically, the entire object graph — must be immutable in order to be considered immutable.

    • private field arrays marked as readonly are immutable, but that doesn’t mean you can’t replace the indexes with different values. A way to solve this is to return a copy of the array or an enumeration of the array
    • The objects of the array may or may not be immutable. Whether you make them immutable or you return clones of the them or whatever, that’s really up to you.

    In your situation, the objects are integers (which are immutable), but your array itself isn’t (again, depending what you define as immutable). If all you want to guarantee is the private field can’t be altered and don’t care about the indexes, then you’re fine. But, if you want the indexes to be locked in then you need to expose your array in another way.


    Also, a nice collection to look into is the ReadOnlyCollection<T>. It is a collection that holds a reference to the original collection (it wraps it), so that the indexes can’t be changed.


    Also, point in case… already you have varying answers to the degree of what “immutable” actually means.

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

Sidebar

Related Questions

I can't figure out why I'm getting this error. I only have one class
I have a custom class that uses boost mutexes and locks like this (only
Is there a way to have a private readonly field in a class that
I have a class that defines a read-only property that effectively exposes a private
I have a class with only class methods (utility stuff), so my interface is
Given: I have an interface. I have only class that implements that interface. Question:
I have only one class with many instances. Every instance is observer of couple
I have a helper class that should only ever run in a background thread.
I have a class called Property that I only need to display a few
I have a CORE class that pertains only to my specific site, ie, it

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.