title: C# reference a value type
body: <p>I have the main component of my program which runs in the main thread. The program has the ability to save the user's work to a file, however extra meta-information is saved along with the user's work.</p>
  
  <p>Example:</p>
  
  <pre><code>[Serializable]
  public Class UserWork
  {
    // Lots of things in here. Lots of work user has done, etc...
  }
  </code></pre>
  
  <p>In the program, I store a couple things like settings, etc. Let's assume these are <code>bool flag1</code> and <code>bool flag2</code>. I have a structure like this:</p>
  
  <pre><code>[Serializable]
  public class SavedFile
  {
    UserWork userWork;
    bool flag1;
    bool flag2;
  }
  </code></pre>
  
  <p>When I save:</p>
  
  <pre><code>saveData(new SavedFile(userWork, flag1, flag2));
  </code></pre>
  
  <p>When I load:</p>
  
  <pre><code>SavedFile savedFile = SavedFile.Load(path);
  UserWork userWork = savedFile.userWork;
  bool flag1 = savedFile.flag1;
  bool flag2 = savedFile.flag2;
  </code></pre>
  
  <p>So basically, flag1 and flag2 need to be saved with the work, but they don't necessarily belong to with the work. They actually represent how far through the workflow the user is at the time of save.</p>
  
  <p>Now, I have built an AutoSave feature which monitors events which are fired whenever the user makes any modifications. The UserWork class fires events, so this was easy to do. The issue I'm having is that I'm not sure how to send and store the references to the flags in the AutoSave thread.</p>
  
  <p>I have:</p>
  
  <pre><code>public class AutoSave // (This code runs in a separate thread)
  {
    bool flag1;
    bool flag2;
  
    UserWork userWork;
  
    public AutoSave(UserWork userWork, ref bool flag1, ref bool flag2)
    {
      this.flag1 = flag1;
      this.flag2 = flag2;
  
      this.userWork = userWork;
  
      userWork.workUpdated += new WorkUpdatedHandler(save);
    }
  
    void save(object sender, WorkUpdateArgs e)
    {
      saveDate(new SavedFile(userWork, flag1, flag2));
    }
  }
  </code></pre>
  
  <p>When I run my code (which is not the above code, but similar code), the flags are both always <code>false</code> regardless of what their values are in my main thread.</p>
  
  <p>Edit for clarification: The issue is not that I cannot get this to work with other workarounds. For example, I can make the two flags public in the AutoSave class and update the main flags and the autosave flags simultaneously. i.e. <code>flag1 = autoSave.Flag1 = true</code>. Rather, the issue is that this is cumbersome. It means I must remember to do this <em>each and every time</em> any of the flags get updated. Also, it doesn't make sense to be updating several variables with the same data all the time when I can simply have one copy in memory and reference it in several places.</p>