blog

Home / DeveloperSection / Blogs / Object Class in C#

Object Class in C#

Vijay Shukla3458 17-Jun-2013

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.

Example:
 using System;
class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public override bool Equals(object obj)
{
if (obj.GetType() != this.GetType()) return false;
Point other = (Point)obj;
return (this.x == other.x) && (this.y == other.y);
}
public override int GetHashCode()
{
return x ^ y;
}
public override String ToString()
{
return String.Format("({0}, {1})", x, y);
}
public Point Copy()
{
return (Point)this.MemberwiseClone();
}
}
public sealed class App
{
static void Main()
{
Point p1 = new Point(1, 2);
Point p2 = p1.Copy();
Point p3 = p1;
Console.WriteLine(Object.ReferenceEquals(p1, p2));
Console.WriteLine(Object.Equals(p1, p2));
Console.WriteLine(Object.ReferenceEquals(p1, p3));
Console.WriteLine("p1's value is: {0}", p1.ToString());
Console.ReadKey();
}
}
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: -

Object Class in C#

Updated 18-Sep-2014

Leave Comment

Comments

Liked By