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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:56:41+00:00 2026-06-09T02:56:41+00:00

* Solved . Thanks for the explanations guys, I didn’t fully understand the implications

  • 0

*Solved. Thanks for the explanations guys, I didn’t fully understand the implications of using a value type in this situation.

I have a struct that I’m using from a static class. However, the behavior is showing unexpected behavior when I print it’s internal state at runtime. Here’s my struct:

    public struct VersionedObject
    {

        public VersionedObject(object o)
        {
            m_SelectedVer = 0;
            ObjectVersions = new List<object>();
            ObjectVersions.Add(o);
        }

        private int m_SelectedVer;
        public int SelectedVersion
        {
            get
            {
                return m_SelectedVer;
            }

        }

        public List<object> ObjectVersions;//Clarifying:  This is only used to retrieve values,  nothing is .Added from outside this struct in my code.

        public void AddObject(object m)
        {
            ObjectVersions.Add(m);
            m_SelectedVer = ObjectVersions.Count - 1;
        }
    }

Test code

        VersionedObject vo = new VersionedObject(1);
        vo.AddObject(2);//This is the second call to AddObject()
        //Expected value of vo.SelectedVerion:  1
        //Actual value of vo.SelectedVersion:   1

Now, if you test this code in isolation, i.e., copy it into your project to give it a whirl, it will return the expected result.

The problem; What I’m observing in my production code is this debug output:

objectName, ObjectVersions.Count:2, SelectedVer:0,

Why? From my understanding, and testing, this should be completely impossible under any circumstances.

My random guess is that there is some sort of immutability going on, that for some reason a new struct is being instanced via default constructor, and the ObjectVersions data is being copied over, but the m_SelectedVersion is private and cannot be copied into the new struct?
Does my use of Static classes and methods to manipulate the struct have anything to do with it?

I’m so stumped I’m just inventing wild guesses at this point.

  • 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-09T02:56:45+00:00Added an answer on June 9, 2026 at 2:56 am

    Struct is value type. So most likely you are creating multiple copies of your object in your actual code.

    Consider simply changing struct to class as content of your struct is not really good fit for value type (as it is mutable and also contains mutable reference type).

    More on “struct is value type”:

    First check FAQ which have many good answers already.

    Value types are passed by value – so if you call function to update such object it will not update original. You can treat them similar to passing integer value to function: i.e. would you expect SomeFunction(42) to be able to change value of 42?

    struct MyStruct { public int V;}
    void UpdateStruct(MyStruct x)
    {
      x.V = 42; // updates copy of passed in object, changes will not be visible outside.
    }
    ....
    var local = new MyStruct{V = 13}
    UpdateStruct(local); // Hope to get local.V == 42
    if (local.V == 13) {
      // Expected. copy inside UpdateStruct updated,
      // but this "local" is untouched.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand the problem that OSGI solved thanks to this question.... What does OSGi
Problem solved: Thanks guys, see my answer below. I have a website running in
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
This problem has been solved! Thanks a lot to Brad, Denis and junkie! You're
SOLVED (Thanks Regexident) I have an app that passes the file path of a
I'm using mysql/php/apache . I have the following situation: 2 tables where I need
(Solved) Thanks to NetCat: it was a UI XML problem :) Today I have
maybe you guys can help me figure this out: I have a Dictionary and
I solved my problem, but i still can't understand why there was this **
[SOLVED: thanks to bluefeet and Brian I got to build this working query (turned

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.