Object Class in C#

In this blog I am trying to explain the concept of object class in C#.

Object Class

Object Class is the base class, Object Class supports all classes in the .NET Framework class hierarchy and provides low-level services to derived classes. This is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy.

Languages typically do not require a class to declare inheritance from Object because the inheritance is implicit. Because all classes in the .NET Framework are derived from Object, every method defined in the Object class is available in all objects in the system. Derived classes can and do override some of these methods, including:

·         Equals - Supports comparisons between objects.

·         Finalize - Performs cleanup operations before an object is automatically reclaimed.

·         GetHashCode - Generates a number corresponding to the value of the object to support the use of a hash table.

·         ToString - Manufactures a human-readable text string that describes an instance of the class.


1.  using System;

2.  class Point

3.  {

4.  public int x, y;

5.  public Point(int x, int y)

6.  {

7.  this.x = x;

8.  this.y = y;

9.  }

10. public override bool Equals(object obj)

11. {

12. if (obj.GetType() != this.GetType()) return false;

13. Point other = (Point)obj;

14. return (this.x == other.x) && (this.y == other.y);

15. }

16. public override int GetHashCode()

17. {

18. return x ^ y;

19. }

20. public override String ToString()

21. {

22. return String.Format("({0}, {1})", x, y);

23. }

24. public Point Copy()

25. {

26. return (Point)this.MemberwiseClone();

27. }

28. }

29. public sealed class App

30. {

31. static void Main()

32. {

33. Point p1 = new Point(1, 2);

34. Point p2 = p1.Copy();

35. Point p3 = p1;

36. Console.WriteLine(Object.ReferenceEquals(p1, p2));

37. Console.WriteLine(Object.Equals(p1, p2));

38. Console.WriteLine(Object.ReferenceEquals(p1, p3));

39. Console.WriteLine("p1's value is: {0}", p1.ToString());

40. Console.ReadKey();

41. }

42. }

Code Description:-

1.       Using System (include the System namespace).

2.       The Point class is derived from System.Object.

4.       Create the variables.

5.       Create a constructor for Point class with two parameters.

7.       Set value in its x variable from local variable x value.

8.       Set value in its y variable from local variable y value.

10.   In this line of code override the Equals method.

12.   If this and obj do not refer to the same type, then they are not equal.

13.   Return true if  x and y fields match.

19.   Return the XOR of the x and y fields.

22.   Return the point's value as a string.

26.   Return a copy of this point object by making a simple field copy.

29.   Create sealed class with App name.

31.   Create Main method.

33.   Construct a Point object.

34.   Make another Point object that is a copy of the first.

35.   Make another variable that references the first Point object.

36.   The line below displays false because p1 and p2 refer to two different objects.

37.   The line below displays true because p1 and p2 refer to two different objects that have the same value.

38.   The line below displays true because p1 and p3 refer to one object.

39.   The line below displays: p1's value is: (1, 2).

Output: -

Leave Comment