Le code

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
namespace ArraySorting
{
    public class Employee: IComparable
    {
        public string name;
        public int level;
        public DateTime hiringDate;
 
        public Employee(string name, int level, DateTime hiringDate)
        {
            this.name = name;
            this.level = level;
            this.hiringDate = hiringDate;
        }
 
        public int CompareTo(Object anObject)
        {
            if (anObject == null) return 1;
            if (!(anObject is Employee))
            {
                throw new ArgumentException();
            }
 
            Employee anEmployee = (Employee)anObject;
            if (level < anEmployee.level) return -1;
            else
            {
                if (level == anEmployee.level)
                {
                    if (hiringDate < anEmployee.hiringDate)
                        return -1;
                    else
                    {
                        if (hiringDate == anEmployee.hiringDate)
                            return 0;
                        else return 1;
                    }
                }
                else return 1;
            }
        }
    }
    public class ArraySort
    {
        public static void Main()
        {
            // Create and initialize a new Array instance.
            Employee[] myEmployees = new Employee[10];
            myEmployees[0] = new Employee("a", 2, new DateTime(1990, 1, 1));
            myEmployees[1] = new Employee("b", 2, new DateTime(2000, 1, 1));
            myEmployees[2] = new Employee("c", 2, new DateTime(1990, 1, 1));
            myEmployees[3] = new Employee("d", 4, new DateTime(2000, 1, 1));
            myEmployees[4] = new Employee("e", 4, new DateTime(1990, 1, 1));
            myEmployees[5] = new Employee("f", 4, new DateTime(2000, 1, 1));
            myEmployees[6] = new Employee("g", 1, new DateTime(1990, 2, 5));
            myEmployees[7] = new Employee("h", 1, new DateTime(2000, 1, 1));
            myEmployees[8] = new Employee("i", 1, new DateTime(1990, 1, 1));
            myEmployees[9] = new Employee("j", 0, new DateTime(2001, 1, 1));
 
            // Display the values of the Array.
            Console.WriteLine("The Array instance initially contains values:");
            PrintIndexAndValues(myEmployees);
 
            // Sort the values of the Array.
            Array.Sort(myEmployees);
            // Display the values of the Array.
            Console.WriteLine("After sorting:");
            PrintIndexAndValues(myEmployees);
        }
        public static void PrintIndexAndValues(Array myEmployees)
        {
            foreach (Employee e in myEmployees)
            {
                Console.WriteLine("name: {0} \tlevel: {1} \tdate:{2:d}", e.name, e.level, e.hiringDate);
            }
        }
    }
}
Voila étant débutant, ( et oui débutant toujours depuis 2 semaines ^^) je suis les cours c#, et dans le syllabus, voici le code. il trie des employé suivant le level.

VOila ce que je ne comprends pas, et ne trouve pas d'expliquation clair (ou alors mal cherché)

1. la notation public class Employee: IComparable
cela signifie quoi? que employee herite de icomparable?

2. cette methode public int CompareTo(Object anObject), je ne l'appel jamais mais pourtant l'enlever est pas possible il me marque un message d'erreur. et à quoi sert elle exactement?

3. throw ce petit mot tout gentil à quoi il correspond?

EDIT: JE vois que IComparable fait partie du using system; savez vous ou je pourrais trouvé un récapitulatif de ce que font, c'est élémént.

Merci d'avance

Isarian