.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

System.Object Class: a look on mother of all managed classes in .NET Framework

Posted By:Dhananjay Kumar       Posted Date: February 03, 2011    Points: 75    Category: .Net Framework    URL: http://www.dotnetspark.com  

In this article we will walkthrough and understand each function of Object class available in .NET Framework.
 

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

In this article we will walk through 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.



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; 
        }
    }

}


 

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



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);
 
 
        }
    }
}



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);
 
 
        }
    }
}





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();
 
        }
    }
}



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; }
       
       
    }
 
}




 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend