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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:55:36+00:00 2026-05-16T21:55:36+00:00

I was just curious to know why structs, strings etc are immutable? What is

  • 0

I was just curious to know why structs, strings etc are immutable? What is the reason for making them immutable and rest of the objects as mutable. What are the things that are considered to make an object immutable?

Is there any difference on the way how memory is allocated and deallocated for mutable and immutable objects?

  • 1 1 Answer
  • 2 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-16T21:55:37+00:00Added an answer on May 16, 2026 at 9:55 pm

    If this subject interests you, I have a number of articles about immutable programming at https://ericlippert.com/2011/05/26/atomicity-volatility-and-immutability-are-different-part-one/

    I was just curious to know why structs, strings etc are immutable?

    Structs and classes are not immutable by default, though it is a best practice to make structs immutable. I like immutable classes too.

    Strings are immutable.

    What is the reason for making them immutable and rest of the objects as mutable.

    Reasons to make all types immutable:

    • It is easier to reason about objects that do not change. If I have a queue with three items in it, I know it is not empty now, it was not empty five minutes ago, it will not be empty in the future. It’s immutable! Once I know a fact about it, I can use that fact forever. Facts about immutable objects do not go stale.

    • A special case of the first point: immutable objects are much easier to make threadsafe. Most thread safety problems are due to writes on one thread and reads on another; immutable objects don’t have writes.

    • Immutable objects can be taken apart and re-used. For example, if you have an immutable binary tree then you can use its left and right subtrees as subtrees of a different tree without worrying about it. In a mutable structure you typically end up making copies of data to re-use it because you don’t want changes to one logical object affecting another. This can save lots of time and memory.

    Reasons to make structs immutable

    There are lots of reasons to make structs immutable. Here’s just one.

    Structs are copied by value, not by reference. It is easy to accidentally treat a struct as being copied by reference. For example:

    void M()
    {
        S s = whatever;
        ... lots of code ...
        s.Mutate();
        ... lots more code ...
        Console.WriteLine(s.Foo);
        ...
    }
    

    Now you want to refactor some of that code into a helper method:

    void Helper(S s)
    {
        ... lots of code ...
        s.Mutate();
        ... lots more code ...
    }
    

    WRONG! That should be (ref S s) — if you don’t do that then the mutation will happen on a copy of s. If you don’t allow mutations in the first place then all these sorts of problems go away.

    Reasons to make strings immutable

    Remember my first point about facts about immutable structures staying facts?

    Suppose string were mutable:

    public static File OpenFile(string filename)
    {
        if (!HasPermission(filename)) throw new SecurityException();
        return InternalOpenFile(filename);
    }
    

    What if the hostile caller mutates filename after the security check and before the file is opened? The code just opened a file that they might not have permission to!

    Again, mutable data is hard to reason about. You want the fact "this caller is authorized to see the file described by this string" to be true forever, not until a mutation happens. With mutable strings, to write secure code we’d constantly have to be making copies of data that we know do not change.

    What are the things that are considered to make an object immutable?

    Does the type logically represent something that is an "eternal" value? The number 12 is the number 12; it doesn’t change. Integers should be immutable. The point (10, 30) is the point (10, 30); it doesn’t change. Points should be immutable. The string "abc" is the string "abc"; it doesn’t change. Strings should be immutable. The list (10, 20, 30) doesn’t change. And so on.

    Sometimes the type represents things that do change. Mary Smith’s last name is Smith, but tomorrow she might be Mary Jones. Or Miss Smith today might be Doctor Smith tomorrow. The alien has fifty health points now but has ten after being hit by the laser beam. Some things are best represented as mutations.

    Is there any difference on the way how memory is allocated and deallocated for mutable and immutable objects?

    Not as such. As I mentioned before though, one of the nice things about immutable values is that something you can re-use parts of them without making copies. So in that sense, memory allocation can be very different.

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

Sidebar

Related Questions

Just curious to know that if my PC is infected with a Trojan or
I am just curious to know that what is ClientBase class in WCF, and
I am just curious to know if there is a specific reason why the
I am just curious to know the reason why it's not possible to host
I am just curious to know that why the return type of an asynchronous
I am just curious to know that why default css doesn't works for all
I'm just curious to know about this.When i heard about Spring.net and tried some
I'm just curious to know if anyone has used (successfully) Fusion Charts with Open
This is really just sort of an academic question, I'm just curious to know
just curious if you know of any way to setup a drag boundary for

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.