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.
Let us examine each function one by one.
For all explanation we will use
Student class. Student class is as below,
5 | public string RollNumber { get ; set ; } |
7 | 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.
03 | namespace ConsoleApplication8 |
11 | static void Main( string [] args) |
15 | Student student = new Student { RollNumber = "1" }; |
17 | int hash1 = student.GetHashCode(); |
19 | Console.WriteLine(hash1); |
21 | Console.ReadKey( true ); |
31 | public string RollNumber { get ; set ; } |
33 | public string Name { get ; set ; } |
35 | public override int GetHashCode() |
41 | 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.
02 | using System.Reflection; |
04 | namespace ConsoleApplication8 |
08 | static void Main( string [] args) |
11 | Object obj = new Object(); |
12 | Type type = obj.GetType(); |
13 | foreach (MethodInfo mn in type.GetMethods()) |
15 | Console.WriteLine(mn.Name + " " + mn.ReturnType); |
17 | 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.
03 | using System.Reflection; |
05 | namespace ConsoleApplication8 |
13 | static void Main( string [] args) |
17 | Object obj = new Object(); |
19 | Console.WriteLine(obj.ToString()); |
21 | 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.
02 | using System.Reflection; |
04 | namespace ConsoleApplication8 |
08 | static void Main( string [] args) |
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 ); |
22 | public string RollNumber { get ; set ; } |
23 | public string Name { get ; set ; } |
25 | public override bool Equals( object obj) |
30 | 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.
02 | using System.Reflection; |
04 | namespace ConsoleApplication8 |
08 | static void Main( string [] args) |
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 ); |
22 | public string RollNumber { get ; set ; } |
23 | public string Name { get ; set ; } |
Posted by Dhananjay Kumar ⋅ February 3, 2011
⋅
No comments :
Post a Comment