-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path_12_OO_TheObjectClass.cs
92 lines (82 loc) · 2.74 KB
/
_12_OO_TheObjectClass.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
namespace OurCompany.LearnCoding.OOP.TheObjectClass;
/*
# The object class is the root of the C# class hierarchy.
It is the base class for all types in C#, including
value types and reference types.
# The object class is defined in the System namespace.
# It provides a set of methods and properties that are
common to all objects.
# It defines the following methods which can be overridden by subclasses:
# Equals
# GetHashCode
# ToString
# It also defines the GetType method, which returns the type
of the current instance.
# Class Content:
# Explain the code
# Run the code and review the results
# Uncomment the override methods from MyClass
# Run the code and review the results
*/
public class ThObjectClassApp
{
public static void Main(string[] args)
{
object[] objects = new object[10];
objects[0] = new object();
objects[1] = "Jose Santos";
objects[2] = 5;
objects[3] = 10.45;
objects[4] = true;
objects[5] = new MyClass("Leila", "Rodrigues");
objects[6] = '#';
objects[7] = new MyClass("Jose", "Santos");
objects[8] = "Jose Santos";
objects[9] = new MyClass("Jose", "Santos");
for (int i = 0; i < objects.Length; i++)
{
Console.Write($"Type:{objects[i].GetType()}; ");
Console.Write($"ToString(): {objects[i]}; ");
// string stringRepresentation = objects[i].ToString();
Console.WriteLine($"HashCode: {objects[i].GetHashCode()}");
}
Console.WriteLine($"[1] == [2]: {objects[1] == objects[2]}");
Console.WriteLine($"[1] == [8]: {objects[1] == objects[8]}");
Console.WriteLine($"[5] == [9]: {objects[5] == objects[9]}");
Console.WriteLine($"[7] == [9]: {objects[7] == objects[9]}");
Console.WriteLine($"[1].Equals([2]): {objects[1].Equals(objects[2])}");
Console.WriteLine($"[1].Equals([8]): {objects[1].Equals(objects[8])}");
Console.WriteLine($"[5].Equals([9]): {objects[5].Equals(objects[9])}");
Console.WriteLine($"[7].Equals([9]): {objects[7].Equals(objects[9])}");
Console.WriteLine($"[7].Equals([9]): {objects[7].Equals(objects[8])}");
}
}
public class MyClass
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get => $"{FirstName} {LastName}"; }
public MyClass(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public override string ToString()
{
return $"[First Name: {FirstName}; Last Name :{LastName}]";
}
public override bool Equals(object otherObject)
{
if (otherObject is not MyClass)
{
return false;
}
MyClass other = (MyClass)otherObject;
return FullName.Equals(other.FullName);
}
public override int GetHashCode()
{
return FullName.GetHashCode();
}
}