C# Object Class
- Object Class is the ultimate base class of all kinds of classes or class hierarchies in the .NET framework.
- All the methods in Object Class can be used in any classes and the virtual methods of Object Class can be overridden in any classes.
- If a class is defined without inheriting from any other class explicitly, the class will inherit from Object Class implicitly.
For example:
public class A
{
...
}
It is equivalent to the following.
public class A : Object
{
...
}
Example 01-58-01
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
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
using System; public class Rectangle { private int length; private int width; public Rectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return length * width; } } public class TestRectangle { public static void Main() { Rectangle a = new Rectangle(5, 6); Rectangle b = new Rectangle(5, 6); Console.WriteLine(a.Equals(b) ? "a equals b." : "a doesn't equal b."); Console.WriteLine(a.ToString()); Console.WriteLine("Hash Code = {0}", a.GetHashCode()); Console.Read(); } }
Output
a doesn't equal b. Rectangle Hash Code = 46104728
Explanation
- Line 3-18: Define public class Rectangle which implicitly inherited from Object class.
- Line 8-12: Instructor with 2 parameters.
- Line 20-32: Define a class TestRectangle implicitly inherited from Object class.
- Line 22: Main method starts here.
- Line 24-25: Create instances of the class Rectangle with the same initial values.
- Line 27: Equals() method was defined in Object class and is inherited in Rectangle class. Because a and b are all references. We'll use the method to test for reference equality.
- Line 28: ToString() is also a method defined in Object class and inherited in Rectangle.
- Line 29: GetHashCode() was defined in Object class as well and return the hash code of the reference.
Example 01-58-02
We'll add 3 override methods for the class Rectangle in the example 01-58-01
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
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
using System; public class Rectangle { private int length; private int width; public Rectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return length * width; } public override bool Equals(object obj) { if (this.GetType() == obj.GetType()) { Rectangle that = (Rectangle)obj; return this.length == that.length && this.width == that.width; } else { return false; } } public override int GetHashCode() { return area(); } public override string ToString() { return "Rectangle: Length=" + this.length.ToString() + ", Width=" + this.width.ToString() + ", Area=" + area().ToString(); } } public class TestRectangle { public static void Main() { Rectangle a = new Rectangle(5, 6); Rectangle b = new Rectangle(5, 6); Console.WriteLine(a.Equals(b) ? "a equals b." : "a doesn't equal b."); Console.WriteLine(a.ToString()); Console.WriteLine("Hash Code = {0}", a.GetHashCode()); Console.Read(); } }
Output
a equals b. Rectangle: Length=5, Width=6, Area=30 Hash Code = 30
Explanation
- Line 19-30: Define a method Equals() to override the same virtual method in Object class.
- Line 21: GetType() is defined in Object class to return the type of the reference.
- Line 23: Explicitly cast obj to Rectangle type.
- Line 24: Return true if the field length and width of the 2 references are equal at the same time.
- Line 50: a.Equals(b) returns true because the override method is called.
- Line 32-35: Define the override method GetHashCode() to return the area of the Rectangle.
- Line 37-40: Define the override method ToString().
- Line 51-52: The override methods are called instead of the original methods in Object class.
- The result is different from that in the example 01-58-01 because the override methods are called.