title: Inheriting C# Tuple<T, T> while adding value checks to the constructor.
body: <p>I am not sure how to phrase the question better. I searched for it and did not find much of an answer, whether in Stack Overflow or elsewhere. Everything I got was how terrible the <code>Tuple</code> type is and how I should avoid doing that at all costs.</p>
  
  <p>Anyway, the program I am working on, essentially, models a house with the rooms in it as some sort of Graph. To represent the rooms that should be adjacent to each other (and edge, or an <code>Adjacency</code>), I am using a <code>Tuple&lt;Room, Room&gt;</code> . However, every time I create that <code>Tuple</code> I go through checks to make sure the same two rooms are always in the same order. So if this was my class</p>
  
  <pre><code>public class Adjacency : Tuple&lt;Room, Room&gt;
  { 
      public Adjacency(Room item1, Room item2)
      {
      }
  }
  </code></pre>
  
  <p>I need <code>Adjacency(Room1, Room2);</code> and  <code>Adjacency(Room2, Room1);</code> to always produce the same object. I am currently doing that by checking <em>before</em> I create the <code>Tuple</code> using <code>Tuple.Create</code>, but the code seems a bit of a mess at the moment and I am trying to abstract by inheriting from <code>Tuple&lt;Room, Room&gt;</code> and doing the checks in the constructor. (However since the base constructor executes first I can't find a way to do that).</p>
  
  <p>The code I want to simplify/abstract is what follows, all currently in class <code>House</code>:</p>
  
  <pre><code>    public List&lt;Tuple&lt;Room, Room&gt;&gt; Adjacencies { get; private set; }
  
      public void PairRooms(Room r1, Room r2)
      {
          if(r1.UniqueID == r2.UniqueID)
              throw new InvalidOperationException("Cannot pair a room with itself.");
  
          foreach(var pair in Adjacencies)
              if((r1.UniqueID == pair.Item1.UniqueID &amp;&amp; r2.UniqueID == pair.Item2.UniqueID)
                  || (r1.UniqueID == pair.Item2.UniqueID &amp;&amp; r2.UniqueID == pair.Item1.UniqueID))
                  throw new Exception("Pair already paired.");
  
          var adjacentRooms = r1.NumericID &lt; r2.NumericID ? Tuple.Create(r1, r2) : Tuple.Create(r2, r1);
          Adjacencies.Add(adjacentRooms);
          Console.WriteLine($"{r1.Name}, {r2.Name} are paired");
      }
  
      public IList&lt;Room&gt; GetAdjacentRooms(Room room)
      {
          var rooms = new List&lt;Room&gt;();
          foreach(var pair in Adjacencies)
              if(pair.Item1.UniqueID == room.UniqueID)
                  rooms.Add(pair.Item2);
              else if(pair.Item2.UniqueID == room.UniqueID)
                  rooms.Add(pair.Item1);
  
          return rooms;
      }
  
      public bool AreAdjacent(Room r1, Room r2)
      {
          foreach(var pair in Adjacencies)
              if((r1.UniqueID == pair.Item1.UniqueID &amp;&amp; r2.UniqueID == pair.Item2.UniqueID)
                  || (r1.UniqueID == pair.Item2.UniqueID &amp;&amp; r2.UniqueID == pair.Item1.UniqueID))
                  return true;
  
          return false;
      }
  </code></pre>
  
  <p>I know the code is a mess and I am using two ID's for the objects (one is a GUID and the other is just based on a static population counter), but that's not the part I am having trouble with.</p>
  
  <p>I am using the <code>PairRooms</code> method all over the <code>House</code> constructor, and the other two methods are used by algorithms operating on the house (to shuffle the rooms about and whatnot.)</p>
  
  <p>Thanks for your help.</p>
  
  <p>PS. would it be a viable alternative to have <code>Tuple&lt;Room, Room&gt;</code> a private property of <code>Adjacency</code> instead of it being the base class ?</p>