עליך להתחבר בכדי להוריד קבצים מהאתר
על מנת להבטיח שמשאבינו לא ינוצלו לרעה.
1 : class Person
2 : {
3 : private int id;
4 : private string firstName;
5 : private string lastName;
6 : private byte age;
7 :
8 : public Person()
9 : {
10 : }
11 : public Person(string first, string last, byte age, int id)
12 : {
13 : this.firstName = first;
14 : this.lastName = last;
15 : this.age = age;
16 : this.id = id;
17 : }
18 : public void SetPerson(string first, string last, byte age, int id)
19 : {
20 : this.firstName = first;
21 : this.lastName = last;
22 : this.age = age;
23 : this.id = id;
24 : }
25 : public void PrintPerson()
26 : {
27 : Console.WriteLine("Name:{0} {1}", lastName, firstName);
28 : Console.WriteLine("ID : {0}", id);
29 : Console.WriteLine("age : {0}", age);
30 : }
31 : public override bool Equals(Object obj)
32 : {
33 : if (obj is Person)
34 : {
35 : Person p;
36 : p = (Person)obj;
37 : if (this.firstName == p.firstName && this.lastName == p.lastName && this.age == p.age && this.id == p.id)
38 : return true;
39 : }
40 : return false;
41 : }
42 : public override int GetHashCode()
43 : {
44 : return this.id;
45 : }
46 : public override string ToString()
47 : {
48 : return lastName + "," + firstName + "," + age;
49 : }
50 : }
1 : class Program
2 : {
3 : static void Main(string[] args)
4 : {
5 : Person p1 = new Person("Moshe", "Moshe", 12, 2345);
6 : Person p2 = new Person("Nitza", "Nitza", 34, 678);
7 : Person p3 = new Person("Nitza", "Nitza", 34, 678);
8 :
9 : Console.WriteLine(p1.GetType());
10 : Console.WriteLine(p1.GetType().BaseType);
11 :
12 : if (p1.Equals(p2))
13 : Console.WriteLine("Objects are equals");
14 : else
15 : Console.WriteLine("Objects are not equals");
16 : if (p2.Equals(p3))
17 : Console.WriteLine("Objects are the equals");
18 : else
19 : Console.WriteLine("Objects are not equals");
20 : Console.WriteLine(p1.ToString());
21 : }
22 : }