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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:16:17+00:00 2026-05-23T13:16:17+00:00

I am reading Eric Liperts’ blog about Mutating Readonly Structs and I see many

  • 0

I am reading Eric Liperts’ blog about Mutating Readonly Structs and I see many references here in SO to this blog as an argument why value types must be immutable.
But still one thing is not clear, says that when you access value type you always get the copy of it and here is the example :

struct Mutable
{
    private int x;
    public int Mutate()
    {
        this.x = this.x + 1;
        return this.x;
    }
}

class Test
{
    public readonly Mutable m = new Mutable();
    static void Main(string[] args)
    {
        Test t = new Test();
        System.Console.WriteLine(t.m.Mutate());
        System.Console.WriteLine(t.m.Mutate());
        System.Console.WriteLine(t.m.Mutate());
    }
}

And the question is this why when I change the

public readonly Mutable m = new Mutable();

to

public Mutable m = new Mutable();

everything starts to work es expected.

Please can you explain more clear why Value Types must be immutable.
I know that it is good for thread safety, but in this case same can be applied to reference types.

  • 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-23T13:16:18+00:00Added an answer on May 23, 2026 at 1:16 pm

    Structs with mutating methods behave strangely in several situations.

    The example you already discovered is a readonly field. A defensive copy is necessary because you don’t want to mutate a readonly field.

    But also when used as properties. Once again an implicit copy happens, and only the copy is mutated. Even if the property has a setter.

    struct Mutable
    {
        private int x;
        public int Mutate()
        {
            this.x = this.x + 1;
            return this.x;
        }
    }
    
    Mutable property{get;set;}
    
    void Main()
    {
        property=new Mutable();
        property.Mutate().Dump();//returns 1
        property.Mutate().Dump();//returns 1 :(
    }
    

    This shows that mutating methods are problematic on structs. But it doesn’t show that a mutable struct with either public fields or properties that have a setter is problematic.

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

Sidebar

Related Questions

Reading this question I found this as (note the quotation marks) code to solve
Reading over the responses to this question Disadvantages of Test Driven Development? I got
Reading MSDN (and other sources) about custom report items (CRI) for reporting services 2005.
Reading through this question on multi-threaded javascript, I was wondering if there would be
Reading this post has left me wondering; are nightly builds ever better for a
After reading Eric Evans' Domain driven Design I have a few questions. I searched
Recently finished reading Eric Evans Domain Driven Design (very informative, very enjoyable) however come
I'm reading through Eric Evans' awesome work, Domain-Driven Design. However, I can't help feeling
I'm reading CSS the definitive guide by Eric Meyer 3rd edition. His section on
I'm reading a book by Eric Roberts - Art and science of java and

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.