[Mono-list] About Arrays of X

yoros@wanadoo.es yoros@wanadoo.es
Sat, 14 Dec 2002 17:33:24 +0100


--tpyx7gKuSYt+mjHM
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable


Thank you.
It's a good way to get genericity.

See you,

    Pedro


On Sat, Dec 14, 2002 at 10:58:21AM -0500, Jonathan Pryor wrote:
> 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:
>=20
>         using System;
>         using System.Collections;
>=20
>         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});
>           }
>          =20
>           private static void Algorithm (Array a) {
>             foreach (object o in a)
>               Console.WriteLine (o);
>           }
>=20
>           private static void Another (IList a) {
>             foreach (object o in a)
>               Console.WriteLine (o);
>           }
>         }
>=20
> Hope this helps...
>  - Jon
>=20
> On Sat, 2002-12-14 at 07:37, yoros@wanadoo.es wrote:
> > Hi,
> >=20
> > 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:
> >=20
> > error CS0030: Cannot convert type 'int[]' to 'System.IComparable[]'
> >=20
> > I tried to do the same thing in Java (using Integer[] and Comparable[])
> > and it worked.
> >=20
> > I think this feature must be in all object oriented languages. In the
> > fact, Java, C++, Eiffel, etc... has the feature.
> >=20
> > Can anyone tell me how can I make the construction I want ? (int[]
> > inside one IComparable[] to get one generic class).
> >=20
> > Regards,
> >=20
> >     Pedro
>=20

--=20
Pedro Martinez Juli=E1
\  yoros@terra.es
)|    yoros@wanadoo.es
/        http://yoros.cjb.net
Socio HispaLinux #311
Usuario Linux #275438 - http://counter.li.org
GnuPG public information:  pub  1024D/74F1D3AC
Key fingerprint =3D 8431 7B47 D2B4 5A46 5F8E  534F 588B E285 74F1 D3AC

--tpyx7gKuSYt+mjHM
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE9+11UWIvihXTx06wRAmBCAKCP5+qbqGOG2X6Kl1aCjCN2qvaagACcDYkx
vMZxXGI2RlbZo36KaIM6phI=
=vT5z
-----END PGP SIGNATURE-----

--tpyx7gKuSYt+mjHM--