System.Object : a look on mother of all managed classes

In this article we will walkthrough and understand each function of Object class.

System.Object is known as mother class of all the managed classes. All the classes are inherited from System.Object. It is base type for all the managed classes. So it defines the base behavior of all the managed classes.

System.Object class contains below functions.

clip_image002

Let us examine each function one by one.

For all explanation we will use Student class. Student class is as below,


 class Student

    {

        public string RollNumber { get; set; }

        public string Name { get; set; }

    }

Object.GetHashCode method

1. This returns an integer.

2. On overriding it returns unique identifier for instance

3. Hash code for a instance can be calculated using simple algorithm to complex algorithm.

4. We should be using a constant and Read only data to calculate the hash code. Because if data changes then hash value will also get changed.

In Employee class let us override the GetHashCode() method. We will be returning 100 times of roll number as hash value for the instance. Feel free to put any complex algorithm in overridden method to calculate hash value of the instance.


using System;

namespace ConsoleApplication8

{

    class Program

    {

        static void Main(string[] args)

        {

            Student student = new Student { RollNumber = "1" };

            int hash1 =   student.GetHashCode();

            Console.WriteLine(hash1);

            Console.ReadKey(true);

        }

    }

    class Student

    {

        public string RollNumber { get; set; }

        public string Name { get; set; }

        public override int GetHashCode()

        {

            //return base.GetHashCode();

            return Convert.ToInt32(RollNumber)*100;

        }

    }

}

clip_image004

Object.GetType method

1. This returns a Type object.

2. This is mainly used for reflection.

3. Type class is an abstract class and inherits MemberInfo class and implements _Type , IReflect interface

clip_image005

4. This class contains many vital methods like Name , ReturnType etc.

In below example, we will print type info of all the functions inside System.Object class.


using System;
using System.Reflection;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {

            Object obj = new Object();
            Type type = obj.GetType();
            foreach(MethodInfo mn in type.GetMethods())
            {
                Console.WriteLine(mn.Name + "  " + mn.ReturnType);
            }
            Console.ReadKey(true);

        }
    }

clip_image007

Object.ToString method

1. This method is one of the widely used methods.

2. This returns string representation of the instance.

3. It returns fully qualified name of the type.

4. For primitive type it is overridden and returns string values.

In below example we will convert instance of Object class to string.


using System;

using System.Reflection;

namespace ConsoleApplication8

{

    class Program

    {

        static void Main(string[] args)

        {

            Object obj = new Object();

            Console.WriteLine(obj.ToString());

            Console.ReadKey(true);

        }

    }

clip_image009

Object.Equals method

1. Two References are equal only if they are pointing to the same object.

2. Two references are not equal if they are pointing to different objects.

3. For value types Equals is overridden and returns the comparison of values

4. Equals method can be override.

In below example we are overriding the Equals() method in Student class. Since we are overriding Equals() method and not overriding GetHashCode(). We should override both the methods to avoid unexpected result.

Two objects are equal if they have same hash value.


using System;
using System.Reflection;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {

            Student s1 = new Student { RollNumber = "1", Name = "Scott" };
            Student s2 = new Student { RollNumber = "1", Name = "Scott" };          
            bool result = s1.Equals(s2);
            Console.WriteLine(result);           
            Console.ReadKey(true);

        }
    }

    class Student
    {
        public string RollNumber { get; set; }
        public string Name { get; set; }

        public override bool Equals(object obj)
        {
            //return base.Equals(obj);
            if (obj == null)
                return false;
            return this.GetHashCode() == -obj.GetHashCode();

        }
    }

}

clip_image011

Object.ReferenceEquals method

1. If two objects are same it returns true.

2. It is not virtual and cannot be overridden.

3. It compares identity of two objects.


using System;
using System.Reflection;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {

            Student s1 = new Student { RollNumber = "1", Name = "Scott" };
            Student s2 = new Student { RollNumber = "1", Name = "Scott" };
            bool result = Student.ReferenceEquals(s1, s2);
            Console.WriteLine(result);           
            Console.ReadKey(true);

        }
    }

    class Student
    {
        public string RollNumber { get; set; }
        public string Name { get; set; }

    }

}

clip_image012

One response to “System.Object : a look on mother of all managed classes”

  1. […] System.Object : a look on mother of all managed classes […]

Leave a comment

Create a website or blog at WordPress.com