[Mono-list] About Arrays of X

Jonathan Pryor jonpryor@vt.edu
14 Dec 2002 10:58:21 -0500


You can always use System.Array as your function argument, as all array
types are implicitly convertible to System.Array.  Or, you could use
System.Collections.IList, as System.Array implements IList, and using
IList allows you to pass in the Collections classes.  For example:

        using System;
        using System.Collections;

        class X {
          public static void Main () {
            Algorithm (new int[]{1, 2, 3, 4, 5});
            Algorithm (new char[]{'a', 'b', 'c', 'd', 'e'});
            Algorithm (new string[]{"foo", "bar", "baz", "qux"});
            Another (new short[]{2, 4, 6, 8, 10});
            Another (new long[]{10, 20, 30, 40, 50});
          }
          
          private static void Algorithm (Array a) {
            foreach (object o in a)
              Console.WriteLine (o);
          }

          private static void Another (IList a) {
            foreach (object o in a)
              Console.WriteLine (o);
          }
        }

Hope this helps...
 - Jon

On Sat, 2002-12-14 at 07:37, yoros@wanadoo.es wrote:
> Hi,
> 
> I was making a little program that implements a few generic algorithms
> for arrays. I decided that the best option was to define the type of the
> arguments of the methods to "IComparable []" (array of IComparable) but
> when I call that method with one "int[]" the compiler tells me the
> following message:
> 
> error CS0030: Cannot convert type 'int[]' to 'System.IComparable[]'
> 
> I tried to do the same thing in Java (using Integer[] and Comparable[])
> and it worked.
> 
> I think this feature must be in all object oriented languages. In the
> fact, Java, C++, Eiffel, etc... has the feature.
> 
> Can anyone tell me how can I make the construction I want ? (int[]
> inside one IComparable[] to get one generic class).
> 
> Regards,
> 
>     Pedro