title: How to create mutually exclusive properties in C#
body: <p>I have a class with three properties:</p>
  
  <pre><code>class PriceCondition
  {
      public Product SalesCode {...}
      public ControlDate Condition {...}
      public PriceDetail Pricing {...}
  }
  </code></pre>
  
  <p>Any instance of <code>PriceCondition</code> can only have EITHER a <code>SalesCode</code> or a <code>Condition</code>.  If <code>Condition</code> is chosen, then <code>Pricing</code> is required, but that's not pertinent to this discussion.</p>
  
  <p>Being a relatively poor programmer, I initially tried the following:</p>
  
  <pre><code>    public Product SalesCode
      {
          get { return _salesCode; }
          set 
          {
              this.Condition = null;
              _salesCode = value; 
          }
      }
  
      public ControlDate Condition
      {
          get { return _cond; }
          set 
          {
              this.SalesCode = null;
              _cond = value; 
          }
      }
  </code></pre>
  
  <p>In hindsight, it's obvious why that created a stack overflow.  Searching for the right way, I found <a href="https://stackoverflow.com/questions/24198649/xor-of-list-of-objects-in-c-sharp">this SO answer about XORing a <code>List</code></a>, but I can't figure out how to apply that to what I'm trying to do, since <code>Except</code> is an <code>IEnumerable</code> method, and I'm not using a <code>List&lt;T&gt;</code> or anything similar.</p>
  
  <p>How can I ensure that only one of the properties is set at any time?  Can I pass in a <code>var CodeOrCondition</code> as a parameter in the constructor, use <code>typeof</code> to figure out what it is, then assign it appropriately?  I only sort of understand what I just said, so having some foreknowledge of whether that's going to work or not would be helpful before I set out to write the code.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>After excellent help found in the answer(s), I ended up with this:</p>
  
  <pre><code>public class PriceCondition
  {
      #region Constructor
      /// &lt;summary&gt;Create an empty PriceCondition.
      /// &lt;/summary&gt;
      /// &lt;remarks&gt;An empty constructor is required for EntityFramework.&lt;/remarks&gt;
      public PriceCondition();
  
      /// &lt;summary&gt;Create a PriceCondition that uses a Sales Code.
      /// &lt;/summary&gt;
      /// &lt;param name="salesCode"&gt;The Product to use.&lt;/param&gt;
      public PriceCondition(Product salesCode)
      {
          SalesCode = salesCode;
          Condition = null;
          Pricing = null;
      }
  
      /// &lt;summary&gt;Create a PriceCondition that uses a DateControl and Price (e.g. "0D7")
      /// &lt;/summary&gt;
      /// &lt;param name="dateControl"&gt;The DateControl Condition to use.&lt;/param&gt;
      /// &lt;param name="price"&gt;The PriceDetail to use.&lt;/param&gt;
      public PriceCondition(Condition dateControl, PriceDetail price)
      {
          SalesCode = null;
          Condition = dateControl;
          Pricing = price;
      }
      #endregion
  ....
  }
  </code></pre>
  
  <p><strong>Second Update:</strong></p>
  
  <p>EntityFramework got in my way.  I knew that it required empty constructors, but didn't realize the depth of why.  We're finding that using <code>private set</code> is keeping EF from populating objects from the database.  At least that's what it looks like.  We're persisting the data in our <strong>DBInitializer</strong>, but the information isn't getting loaded into <code>PriceCondition</code> when we try to use it.  The easy answer seemed to be putting the setters back to a standard backing-store method and relying on the business logic layer to never set a <code>SalesCode</code> and a <code>ControlDate</code> on the same <code>PriceCondition</code>.  But now that's not working either.  We'll bang away at it more, but any suggestions would be really appreciated.</p>