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

The Archive Base Latest Questions

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

The best way to explain my problem is with a code snippet: enum Resource

  • 0

The best way to explain my problem is with a code snippet:

enum Resource { ..., Size }
class ResourceVector 
{
    int[] values = new int[(int)Resource.Size];

    public static ResourceVector operator + (ResourceVector a, ResourceVector b)
    {...}

    ...
}

We are using this type everywhere as though it were a value type. I.e. we are making the assumption that:

ResourceVector b = a;
b += c;

will not affect a, because that is how we are used to talking about vectors (and that is how a vector with a fixed number of fields does behave, if implemented as a struct).

However since that assumption is wrong, it has led to some extremely subtle bugs.

I am wondering if there is a way to make this behave as a value type, other than just expanding the members of Resource each into their own members in a struct ResourceVector (which requires touching every member of ResourceVector if we wish to add another Resource).

Oh, just in case, we are working with C# 2.0. So no fancy-pants features 🙂

Thanks.

  • 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-16T06:23:01+00:00Added an answer on May 16, 2026 at 6:23 am

    I think you are confusing “value type” with “immutable type”. You actually want your ResourceVector to be an immutable type.

    An immutable type is one that has no way to change its contents. Once constructed, an instance retains its value until it is garbage-collected. All operations such as addition return a new instance containing the result instead of modifying an existing instance.

    string is the most well-known immutable class. All operations like Substring and Replace return a new string and don’t modify the existing strings.

    Once your type is properly immutable, semantically it doesn’t matter so much anymore whether it is a value type or a reference type — but it matters to the performance. If you pass values around a lot, you should probably make it a reference type in order to avoid a lot of unnecessary copying.

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

Sidebar

Related Questions

I'll try to explain this problem the best way i can with code: double
The best way to explain my problem is by a example. I have a
the best way to explain is with example so: this is the model public
I'm having this problem with this piece of PHP code: class Core { public
The best way for me to explain my problem is to basically just give
The best way to explain my problem is probably just to show you my
Having issues with Twitter Bootstrap responsive navigation. The best way to explain this is
The best way I think to explain this is to tell you what my
can someone explain the best way to get around the following, rather curious type
Ok so let me try to explain this the best way that i can.

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.