title: `Type.GetProperties` property order
body: <p><strong>Short Version</strong></p>
  
  <p>The MSDN documentation for <a href="http://msdn.microsoft.com/en-us/library/kyaxdd3x.aspx" rel="noreferrer"><code>Type.GetProperties</code></a> states that the collection it returns is not guaranteed to be in alphabetical or declaration order, though running a simple test shows that in general it is returned in declaration order. Are there specific scenarios that you know of where this is not the case? Beyond that, what is the suggested alternative?</p>
  
  <p><strong>Detailed Version</strong></p>
  
  <p>I realize the MSDN documentation for <a href="http://msdn.microsoft.com/en-us/library/kyaxdd3x.aspx" rel="noreferrer"><code>Type.GetProperties</code></a> states:</p>
  
  <blockquote>
    <p>The GetProperties method does not return properties in a particular
    order, such as alphabetical or declaration order. Your code must not
    depend on the order in which properties are returned, because that
    order varies.</p>
  </blockquote>
  
  <p>so there is no guarantee that the collection returned by the method will be ordered any specific way. Based on some tests, I've found to the contrary that the properties returned appear in the order they're defined in the type.</p>
  
  <p>Example: </p>
  
  <pre><code>class Simple
  {
      public int FieldB { get; set; }
      public string FieldA { get; set; }
      public byte FieldC { get; set; }
  }
  class Program
  {
      static void Main(string[] args)
      {
          Console.WriteLine("Simple Properties:");
          foreach (var propInfo in typeof(Simple).GetProperties())
              Console.WriteLine("\t{0}", propInfo.Name);
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Simple Properties:
          FieldB
          FieldA
          FieldC
  </code></pre>
  
  <p>One such case that this differs only slightly is when the type in question has a parent who also has properties:</p>
  
  <pre><code>class Parent
  {
      public int ParentFieldB { get; set; }
      public string ParentFieldA { get; set; }
      public byte ParentFieldC { get; set; }
  }
  
  class Child : Parent
  {
      public int ChildFieldB { get; set; }
      public string ChildFieldA { get; set; }
      public byte ChildFieldC { get; set; }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          Console.WriteLine("Parent Properties:");
          foreach (var propInfo in typeof(Parent).GetProperties())
              Console.WriteLine("\t{0}", propInfo.Name);
  
          Console.WriteLine("Child Properties:");
          foreach (var propInfo in typeof(Child).GetProperties())
              Console.WriteLine("\t{0}", propInfo.Name);
  
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Parent Properties:
          ParentFieldB
          ParentFieldA
          ParentFieldC
  Child Properties:
          ChildFieldB
          ChildFieldA
          ChildFieldC
          ParentFieldB
          ParentFieldA
          ParentFieldC
  </code></pre>
  
  <p>Which means the <code>GetProperties</code> method walks up the inheritance chain from bottom up when discovering the properties. That's fine and can be handled as such.</p>
  
  <p>Questions:</p>
  
  <ol>
  <li>Are there specific situations where the described behavior would differ that I've missed?</li>
  <li>If depending on the order is not recommended then what <em>is</em> the recommended approach?</li>
  </ol>
  
  <p>One seemingly obvious solution would be to define a custom attribute which indicates the order in which the properties <em>should</em> appear (Similar to the <code>Order</code> property on the <code>DataMember</code> attribute). Something like:</p>
  
  <pre><code>public class PropOrderAttribute : Attribute
  {
      public int SeqNbr { get; set; }
  }
  </code></pre>
  
  <p>And then implement such as: </p>
  
  <pre><code>class Simple
  {
      [PropOrder(SeqNbr = 0)]
      public int FieldB { get; set; }
      [PropOrder(SeqNbr = 1)]
      public string FieldA { get; set; }
      [PropOrder(SeqNbr = 2)]
      public byte FieldC { get; set; }
  }
  </code></pre>
  
  <p>But as many have found, this becomes a serious maintenance problem if your type has 100 properties and you need to add one between the first 2.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The examples shown here are simply for demonstrative purposes. In my specific scenario, I define a message format using a class, then iterate through the properties of the class and grab their attributes to see how a specific field in the message should be demarshaled. The order of the fields in the message is significant so the order of the properties in my class needs to be significant.</p>
  
  <p>It works currently by just iterating over the return collection from <code>GetProperties</code>, but since the documentation states it is not recommended I was looking to understand why and what other option do I have?</p>