title: C# Method Overloading
body: <p>I have a method that needs to allow a wide variety of input types. There are two categories of arguments: boundary type arguments, and actual data input arguments.</p>
  
  <p>The boundary arguments are for example, order, frequency, number of points, and number of data points. These boundary arguments are of type int and are common, regardless of the actual data input argument type.</p>
  
  <p>The actual input arguments may be of types: byte, int, short, uint, float, double, decimal, long, etc. To complicate matters further, the actual input data may be individual data, or a list or array of that type. So, the actual input may come as List or uint[], etc. This input is ultimately converted to type double - either as single data or double[].</p>
  
  <p>The method consists of three parts: part one checks the validity of the boundary arguments. This part always applies regardles of the actual input data type. Part two checks and processes the input data arguments. This part changes depending of the type of the input data. Part three performs calculations on the data and is once again common.</p>
  
  <p>I have thought about generic, and I have thought about standard overloading with generics but both seem inefficient. I have come up with what I believe is a workable solution but would appreciate comments: Is my approach computationally efficent, and is there a better way to do this. Your comments would be appreciated.</p>
  
  <p>This is what I currently have:</p>
  
  <pre><code>// ... create lists to store data
  static List&lt;double&gt; aList = new List&lt;double&gt;(8);
  static List&lt;double&gt; fList = new List&lt;double&gt;(8);
  
  public static double[] MyMethod(int numPts, int numData, object aValue, object fValue)
  {
      // ... part 1
      if (numData &lt; 2) throw new ArgumentOutOfRangeException("numberData must be &gt;= 2.");
      if (numPts &lt; 2) throw new ArgumentOutOfRangeException("numberPoints must be &gt;= 2.");
      if (numData &lt; numPts) throw new ArgumentOutOfRangeException("numberData must be
          &gt;= numPts.");
  
      // ... part 2
      if (aValue is byte || aValue is short || aValue is int || aValue is long ||
          aValue is float || aValue is double || aValue is decimal ||
          aValue is List&lt;byte&gt; || aValue is byte[] || aValue is List&lt;short&gt; ||
          aValue is short[] || aValue is List&lt;int&gt; || aValue is int[] ||
          aValue is List&lt;float&gt; || aValue is float[] || aValue is List&lt;double&gt; ||
          aValue is double[])
      { }
      else throw new ArgumentException("a values must be of a numeric type.");
  
      double a = 0.0;
      if (aValue is byte || aValue is short || aValue is int || aValue is long ||
          aValue is float || aValue is double || aValue is decimal)
      {
          a = (double)aValue;
          // ... store individual values
          aList.Add(a);
  
          // ... create the x variable vector
          double[] x = aList.ToArray();     // a values
      }
      else if (aValue is List&lt;byte&gt; || aValue is List&lt;short&gt; || aValue is List&lt;int&gt; ||
               aValue is List&lt;float&gt; || aValue is List&lt;double&gt;)
      {
          // ... get the runtime type of the aValue object
          Type t = aValue.GetType();
          // ... convert the aValue object to a generic list
          IList tmp = (IList)Activator.CreateInstance(typeof(List&lt;&gt;).MakeGenericType(t));
          // ... convert the generic list to a strongly typed list
          List&lt;double&gt; aValuesList = tmp.Cast&lt;double&gt;().ToList();
  
          // ... create the x variable vector
          double[] x = aValuesList.ToArray();     // a values
      }
      else
      {
          // ... process the vector input
          // ... get the runtime type of the aValue object
          Type t = aValue.GetType();
          // ... convert the aValue object to an array
          Array tmp = Array.CreateInstance(typeof([]).MakeGenericType(t), aValue.Length);
          // ... convert the generic array to a strongly typed array
          double[] x = tmp.Cast&lt;double&gt;().ToArray();
      }
  
      // ... part 3
      {
          // ... do calculations
      }
  }
  </code></pre>