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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:20:32+00:00 2026-06-17T07:20:32+00:00

I realize that structs are meant to be immutable, that mutating structs is evil,

  • 0

I realize that structs are meant to be immutable, that mutating structs is evil, and the proper way to change values in a struct is by creating new instances. However, I am not clear on the memory and performance aspects/issues of new instances versus allowing the struct to be mutable.

Suppose I have the struct,

struct Vehicle
{
    public readonly int Number;
    public readonly double Speed, Direction;

    public Vehicle(int number, double speed, double direction)
    {
        this.Number = number;
        this.Speed = speed;
        this.Direction = direction;
    }
}

And create it as:

Vehicle car;

as well as

Vehicle plane = new Vehicle(1234, 145.70, 73.20)

If I need to assign to Number, Speed, or Direction later, I could remove the readonly and make the struct mutable – I know it is eveil to do this. – thereby “mutating” an already created struct value.

Alternatively I could create a new struct instance. So instead of saying car.Speed = 120.7; I could say car = new Vehicle(car.Number, 178.55, car.Direction);. That would create a new struct value almost like the old value, only with the Speed changed. But it wouldn’t mutate the existing struct value.

Here is the issue. Suppose, as an extreme example, I need to update the speed and/or the direction many, many thousands of times per second. I would think that the creation of this many instances would severely impact memory and performance, and that in this case it would be better to allow the struct to be mutable.

Can anyone please clarify the memory and performance issues of a mutable struct versus the proper way to implement a struct in this type of an extreme case?

  • 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-17T07:20:33+00:00Added an answer on June 17, 2026 at 7:20 am

    There are two distinct usage cases for structs; in some cases, one wants a type that encapsulates a single value and behaves mostly like a class, but with better performance and a non-null default value. In such cases, one should use what I would call an opaque structure; MSDN’s guidelines for structures are written on the assumption that this is the only usage case.

    In other cases, however, the purpose of the struct is simply to bind some variables together with duct tape. In those cases, one should use a transparent struct which simply exposes those variables as public fields. There is nothing evil about such types. What’s evil is the notion that everything should behave like a class object, or that everything should be “encapsulated”. If the semantics of the struct are such that:

    1. There is some fixed set of readable members (fields or properties) which expose its entire state
    2. Given any set of desired values for those members, one can create an instance with those values (no combinations of values are forbidden).
    3. The default value of the struct should be to have all those members set to the default values of their respective types.

    and any change to them would break the code which uses it, then there is nothing which future versions of the struct could possibly do which a transparent struct would not be able to do, nor is there anything that a transparent struct would allow which a future version of the struct would be able to prevent. Consequently, encapsulation imposes cost without adding value.

    I would suggest that one should endeavor to, whenever practical, make all structs either transparent or opaque. Further, I would suggest that because of deficiencies in the way .net handles struct methods, one should avoid having opaque structures’ public members modify this except perhaps in property setters. It is ironic that while MSDN’s guidelines suggest one shouldn’t use a struct for things that don’t represent a “single value”, in the common scenario where one simply wants to pass a group of variables from one piece of code to another, transparent structs are vastly superior to opaque structs or class types, and the margin of superiority grows with the number of fields.

    BTW, with regard to the original question, I would suggest that it’s useful to represent that your program may want to deal with two kinds of things: (1) a car, and (2) information related to a particular car. I would suggest that it may be helpful to have a struct CarState, and have instances of Car hold a field of type CarState. This would allow instances of Car to expose their state to outside code and possibly allow outside code to modify their state under controlled circumstances (using methods like

    delegate void ActionByRef<T1,T2>(ref T1 p1, ref T2 p2);
    delegate void ActionByRef<T1,T2,T3>(ref T1 p1, ref T2 p2, T3 p3);
    
    void UpdateState<TP1>(ActionByRef<CarState, TP1> proc, ref TP1 p1)
    { proc(ref myState, ref p1); }
    void UpdateState<TP1,TP2>(ActionByRef<CarState, TP1,TP2> proc, ref TP1 p1, ref TP2 p2)
    { proc(ref myState, ref p1, ref p2); }
    

    Note that such methods offer most of the performance advantages of having the car’s state be a mutable class, but without the dangers associated with promiscuous object references. A Car can let outside code may update a car’s state via the above methods without allowing outside code to modify its state at any other time.

    BTW, I really wish .net had a way of specifying that a “safe” struct or class should be considered as encapsulating the members of one or more of its constituents [e.g. so that struct which held a Rectangle called R and an String called Name could be regarded as having a fields X, Y, Width, and Height which alias the corresponding struct fields. If that were possible, it would greatly facilitate situations where a type needs to hold more state than previously expected. I don’t think the present CIL allows for such aliasing in a safe type, but there’s no conceptual reason it couldn’t.

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

Sidebar

Related Questions

I realize that tinyint is a single byte integer (by the way, is it
I realize that TWTweetComposeViewController is new to iOS 5 (which is now a bit
I realize that Go does not have classes but pushes the idea of structs
so I am having some problems with chipmunk, please realize that I am new
I realize that virtual template functions are not allowed in c++. Because of my
I realize that a SO user has formerly asked this question but it was
I realize that keywords and descriptions are old-school SEO techniques and many search engines
I realize that the answer to this question is likely quite obvious (if somewhat
I realize that this question has been asked 100times but none that I have
I realize that it can depend on certain things (and obviously how efficient the

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.