title: What is the fastest way to check a type?
body: <p>Instead of overloading a function 100 times or creating 100 different Comparers for different types I've decided to check the type within one function.</p>
  
  <p>For example I'm using a default comparer to compare values of a set of types (primitives and strings) within 2 objects. It contains the following code:</p>
  
  <pre><code>public class DefComparer : IComparer&lt;object&gt; {
      public int Compare(object a, object b) {
          .... // a = a.GetType().GetField(field).GetValue(a); - not important for the question but I'm just showing that a&amp;b below are different references
          switch (a.GetType().Name) {
              case "Byte":
                  if ((byte)a == (byte)b) return 0;
                  else if ((byte)a &gt; (byte)b) return 1;
                  else return -1;
              case "UInt16":
                  if ((ushort)a == (ushort)b) return 0;
                  else if ((ushort)a &gt; (ushort)b) return 1;
                  else return -1;
              case "SByte":
                  if ((sbyte)a == (sbyte)b) return 0;
                  else if ((sbyte)a &gt; (sbyte)b) return 1;
                  else return -1;
              case "Int16":
                  ...
  </code></pre>
  
  <p>Here I'm using a <code>switch</code> statement that is said to be faster than a chain of <code>if</code>/<code>else</code> statements. But <code>a.GetType().Name</code> returns a string that is dynamically obtained and this method involves string comparisons. That doesn't sound particularly fast to me. I need the Comparer to be as fast as technically possible because It's going to be used on large collections of data.    </p>
  
  <p><b>Q: Is there a faster way to check the type of an object (that does not involve string comparisons)? What is the fastest possible way?</b></p>