The Object Class in .NET

All the types in .NET are represented as objects and are derived from the Object class.


The Object Class in .NET

All the types in .NET are represented as objects and are derived from the Object class.

The Object class has five methods: 

  • GetType
  • Equals
  • ReferenceEquals
  • ToString
  • GetHashCode

Table below show the Object class methods

Method

Description

GetType

Returns type of the object.

Equals

Compares two object instances. Returns true if they are equal, otherwise false.

ReferenceEquals

Compares two object instances. Returns true if both are same instances, otherwise false.

ToString

Converts an instance to a string type.

GetHashCode

Returns hashcode for an object.

GetType

The Type class can be used to get type information from the object. The GetType method of an object returns a type object, which you can use to get information on an object such as its name, namespace, base type and so on.

The following code explains you the GetType method

using
System;
using System.Text;
namespace Object_Class
{
    class Program
    {
        static void Main(string[] args)
        {
            Object ob1 = new Object();
            System.String s1 = "Testing object";

            Type type1 = ob1.GetType();
            Type type2 = s1.GetType();

            // Object class output
            Console.WriteLine(type1.BaseType);
            Console.WriteLine(type1.Name);
            Console.WriteLine(type1.FullName);
            Console.WriteLine(type1.Namespace);
            Console.WriteLine();

            // string output
            Console.WriteLine(type2.BaseType);
            Console.WriteLine(type2.Name);
            Console.WriteLine(type2.FullName);
            Console.WriteLine(type2.Namespace);
            Console.ReadLine();
        }
    }
}

Output of above code:

object_img1.gif

Equal

The Equal method of the Object class can compare two objects.

ReferenceEqual

The Reference method can compare two objects instances.

The following code explains you the Equal method and ReferenceEqual method

using
System;
using System.Text;

namespace
object_class_equals
{
    class Program
    {
        public class Class1 : Object
        {
            public void Method1()
            {
                Console.WriteLine("Method 1");
            }
        }

        public class Class2 : Class1
        {
            public void Method2()
            {
                Console.WriteLine("Method 2");
            }
        }

        static void Main(string[] args)
        {
            Class1 ob1 = new Class1();
            Class2 ob2 = new Class2();     

            string s1 = "String";
            string s2 = "String";

            Console.WriteLine("Coparision of two object");
            Console.WriteLine(Object.Equals(ob1, ob2));
            Console.WriteLine(Object.Equals(s1, s2));

            Console.WriteLine(Object.ReferenceEquals(ob1, ob2));
            Console.WriteLine(Object.ReferenceEquals(ob2, ob2));
            Console.WriteLine(Object.ReferenceEquals(s1, s2));
            Console.ReadLine();
        }
    }
}

Output of the above code:

object_img3.gif

ToString

The ToString method of the Object class converts a type to a string type.

The following code explains you the ToString method

using
System;
using System.Text;

namespace
object_class_string
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "Puran Singh Mehra";
            int age = 36;
            float marks = 89.50f;

            Console.WriteLine(name);
            Console.WriteLine(age.ToString());
            Console.WriteLine(marks.ToString());
            Console.ReadLine();
        }
    }
}

Output of above code:

object_img2.gif

GetHashCode

The GetHashCode method returns the hashcode of an object. To return a hashcode for a type, you must override the GetHashCode method. An integer value is returned which represents whether and object is available in a hashtable.

A hashtable (also commonly known as a man or dictionary) is a data structure that stores one or more key-values pairs of data. Hastable are useful when you want fast access to a list of data through a key (which can be number, letter, string or any object).

The following code explains you the GetHashCode method

using
System;
using System.Text;

namespace
object_Class_Gethashcode
{
    class Program
    {
        class HashC
        {
            string name;
            public HashC(string name)
            {
                this.name = name;
            }

            public override int GetHashCode()
            {
                string n = ToString();
                return n.GetHashCode();                
            }
           
        }

        static void Main(string[] args)
        {
            HashC ob1 = new HashC("Puran Singh Mehra");
            Console.WriteLine(ob1.GetHashCode());
            Console.ReadLine();
        }
    }
}

Output of above code

object_img4.gif

I hope that this article would have helped you in understanding the object class methods in the .NET framework.

Your feedback and constructive contributions are welcome. Please feel free to contact me for feedback or comments you may have about this article.