Sunday 22 January 2012

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



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,
1class Student
2 
3   {
4 
5       public string RollNumber { get; set; }
6 
7       public string Name { get; set; }
8 
9   }
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.
01using System;
02 
03namespace ConsoleApplication8
04 
05{
06 
07    class Program
08 
09    {
10 
11        static void Main(string[] args)
12 
13        {
14 
15            Student student = new Student { RollNumber = "1" };
16 
17            int hash1 =   student.GetHashCode();
18 
19            Console.WriteLine(hash1);
20 
21            Console.ReadKey(true);
22 
23        }
24 
25    }
26 
27    class Student
28 
29    {
30 
31        public string RollNumber { get; set; }
32 
33        public string Name { get; set; }
34 
35        public override int GetHashCode()
36 
37        {
38 
39            //return base.GetHashCode();
40 
41            return Convert.ToInt32(RollNumber)*100;
42 
43        }
44 
45    }
46 
47}
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.
01using System;
02using System.Reflection;
03 
04namespace ConsoleApplication8
05{
06    class Program
07    {
08        static void Main(string[] args)
09        {
10 
11            Object obj = new Object();
12            Type type = obj.GetType();
13            foreach(MethodInfo mn in type.GetMethods())
14            {
15                Console.WriteLine(mn.Name + "  " + mn.ReturnType);
16            }
17            Console.ReadKey(true);
18 
19        }
20    }
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.
01using System;
02 
03using System.Reflection;
04 
05namespace ConsoleApplication8
06 
07{
08 
09    class Program
10 
11    {
12 
13        static void Main(string[] args)
14 
15        {
16 
17            Object obj = new Object();
18 
19            Console.WriteLine(obj.ToString());
20 
21            Console.ReadKey(true);
22 
23        }
24 
25    }
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.
01using System;
02using System.Reflection;
03 
04namespace ConsoleApplication8
05{
06    class Program
07    {
08        static void Main(string[] args)
09        {
10 
11            Student s1 = new Student { RollNumber = "1", Name = "Scott" };
12            Student s2 = new Student { RollNumber = "1", Name = "Scott" };         
13            bool result = s1.Equals(s2);
14            Console.WriteLine(result);          
15            Console.ReadKey(true);
16 
17        }
18    }
19 
20    class Student
21    {
22        public string RollNumber { get; set; }
23        public string Name { get; set; }
24 
25        public override bool Equals(object obj)
26        {
27            //return base.Equals(obj);
28            if (obj == null)
29                return false;
30            return this.GetHashCode() == -obj.GetHashCode();
31 
32        }
33    }
34 
35}
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.
01using System;
02using System.Reflection;
03 
04namespace ConsoleApplication8
05{
06    class Program
07    {
08        static void Main(string[] args)
09        {
10 
11            Student s1 = new Student { RollNumber = "1", Name = "Scott" };
12            Student s2 = new Student { RollNumber = "1", Name = "Scott" };
13            bool result = Student.ReferenceEquals(s1, s2);
14            Console.WriteLine(result);          
15            Console.ReadKey(true);
16 
17        }
18    }
19 
20    class Student
21    {
22        public string RollNumber { get; set; }
23        public string Name { get; set; }
24 
25    }
26 
27}





clip_image012Posted by February 3, 2011

No comments :