Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Générique: comparaison des données

Non,Edité
{filelink=17498}

using System;
 
namespace Générique
{
    class MaClasse : IComparable
    {
        public int val;
 
        public MaClasse(int x)
        {
            val = x;
        }
        public int CompareTo(object obj)
        {
            return val - ((MaClasse)obj).val;
        }
    }
 
    class CompareDemo
    {
        public static bool contains<T>(T what, T[] obs) where T : IComparable
        {
            foreach (T v in obs)
                if (v.CompareTo(what) == 0)
                    return true;
            return false;
        }
 
        public static void Main()
        {
            int[] tabNombres = { 1, 2, 3, 4, 5 };
 
            if (contains(5, tabNombres))
                Console.WriteLine("2 trouvé.");
 
            string[] strs = { "A", "B", "C", "D", "E" };
 
            if (contains("G", strs))
                Console.WriteLine("G non trouvé.");
 
            MaClasse[] tableClasse = { new MaClasse(1), new MaClasse(2),
                      new MaClasse(3), new MaClasse(4), new MaClasse(5) };
 
            if (contains(new MaClasse(2), tableClasse))
                Console.WriteLine("MaClasse(2) trouvé.");
 
        }
    }
 
}

No comments yet.

Leave a Comment