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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:47:52+00:00 2026-05-22T11:47:52+00:00

Is this a proper way to declare immutable structs? public struct Pair { public

  • 0

Is this a proper way to declare immutable structs?

public struct Pair
{
    public readonly int x;
    public readonly int y;

    // Constructor and stuff
}

I can’t think of why this would run into problems, but I just wanted to ask to make sure.

In this example, I used ints. What if I used a class instead, but that class is also immutable, like so? That should work fine too, right?

public struct Pair
{
    public readonly (immutableClass) x;
    public readonly (immutableClass) y;

    // Constructor and stuff
}

(Aside: I understand that using Properties is more generalizable and allows changing, but this struct is intended literally to just store two values. I’m just interested in the immutability question here.)

  • 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-22T11:47:53+00:00Added an answer on May 22, 2026 at 11:47 am

    If you’re going to use structs, it is a best practice to make them immutable.

    Making all the fields readonly is a great way to help (1) document that the struct is immutable, and (2) prevent accidental mutations.

    However, there is one wrinkle, which actually in a strange coincidence I was planning on blogging about next week. That is: readonly on a struct field is a lie. One expects that a readonly field cannot change, but of course it can. “readonly” on a struct field is the declaration writing cheques with no money in its account. A struct doesn’t own its storage, and it is that storage which can mutate.

    For example, let’s take your struct:

    public struct Pair
    {
        public readonly int x;
        public readonly int y;
        public Pair(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public void M(ref Pair p)
        {
            int oldX = x;
            int oldY = y;
            // Something happens here
            Debug.Assert(x == oldX);
            Debug.Assert(y == oldY);
        }
    }
    

    Is there anything that can happen at “something happens here” that causes the debug assertions to be violated? Sure.

        public void M(ref Pair p)
        {
            int oldX = this.x;
            int oldY = this.y;
            p = new Pair(0, 0);
            Debug.Assert(this.x == oldX);
            Debug.Assert(this.y == oldY);
        }
    ...
        Pair myPair = new Pair(10, 20);
        myPair.M(ref myPair);
    

    And now what happens? The assertion is violated! “this” and “p” refer to the same storage location. The storage location is mutated, and so the contents of “this” are mutated because they are the same thing. The struct is not able to enforce the read-only-ness of x and y because the struct doesn’t own the storage; the storage is a local variable that is free to mutate as much as it wants.

    You cannot rely on the invariant that a readonly field in a struct is never observed to change; the only thing you can rely on is that you can’t write code that directly changes it. But with a little sneaky work like this you can indirectly change it all you want.

    See also Joe Duffy’s excellent blog article on this issue:

    http://joeduffyblog.com/2010/07/01/when-is-a-readonly-field-not-readonly/

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

Sidebar

Related Questions

No related questions found

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.