title: c# how to take data_type as input in "foreach" clause
body: <p>I want a code can automatically detect data type and do the foreach loop.
  For example print element in an array one by one. But we don't know the type of this array in advance. I got a solution that use any_array.GetType().ToString() to get string of the data type.
  but this is too stupid.</p>
  
  <p>Is there any more elegant way to do that?</p>
  
  <pre><code>using System;
  
  public class basic_data_structure
  {
      public static void example01()
      {
          int[] int_array = new int[] { 1, 2, 3, 4, 5 }; // type = System.Int32[]
          float[] float_array = new float[] { 1F, 2F, 3F, 4F, 5F }; // type = System.Single[]
          string[] string_array = new string[] { "a", "b", "c", "d", "e" }; // type = System.String[]
  
          print_any_array01(int_array);
          print_any_array01(float_array);
          print_any_array01(string_array);
      }
  
      public static void print_any_array01(Array any_array)
      {
          int count = 0;
          string array_type = any_array.GetType().ToString();
  
          if (array_type == "System.Int32[]")
          {
              foreach (int element in any_array)
              {
                  count += 1;
                  Console.WriteLine("Element #{0}: {1}", count, element);
              }
          }
          else if (array_type == "System.Single[]")
          {
              foreach (float element in any_array)
              {
                  count += 1;
                  Console.WriteLine("Element #{0}: {1}", count, element);
              }
          }
          else if (array_type == "System.String[]")
          {
              foreach (string element in any_array)
              {
                  count += 1;
                  Console.WriteLine("Element #{0}: {1}", count, element);
              }
          }
          else
          {
              Console.WriteLine("ERROR unknown data type array!!!");
          }
      }
  
  }
  </code></pre>