Software/C#.Net

System.Object

charom 2011. 5. 17. 05:02
  • In the .NET universe, every type ultimately derives from a base class named System.Object
    Core Members of System.Object  
 Instance Method of Object Class   Check Points
 Equals() be used to compare object references, not the state of the object
 if you override Equals(), you should also override GetHashCode()
How to check member variables are same or not?
public override bool Equals(object obj) 
{ 
  if (obj is Person && obj != null)  //>>>check null and type<<<
  { 
    Person temp; 
    temp = (Person)obj; 
    if (temp.FirstName == this.FirstName
         && temp.LastName == this.LastName
           && temp.Age == this.Age)  { 
      return true; 
    } else { 
      return false; 
    } 
  } 
  return false; 
} 
Another solution to check equal objects
public override bool Equals(object obj) 
{ 
  // No need to cast 'obj' to a Person anymore, 
  // as everything has a ToString() method. 
  return obj.ToString() == this.ToString(); 
} 
 Finalize() it is called to free any allocated resources before the object is destroyed
check Finalize Methods and Destructors and GC.KeepAlive
 
 GetHashCode() This method returns an int that identifies a specific object instance 
It is important that once you override a Equals() method,
you must overridw GetHasCode() method too
// Return a hash code based on the person's ToString() value. 
public override int GetHashCode() 
{ 
  return this.ToString().GetHashCode(); 
} 
ToString()  returns a string representation of this object, using the <namespace>.<type name> format  .this function can be quite helpful for purpose of debugging(among other reason).a recommended approach is to separate each name/value pair with semicolons and wrap the entire string within square brackets (many types in the .NET base class libraries follow this approach).
public override string ToString() 
{ 
  string myState; 
  myState = string.Format("[First Name: {0}; Last Name: {1}; Age: {2}]",
    FirstName, LastName, Age); 
  return myState; 
} 
 GetType()  This method returns a Type object that fully describes the object you are currently referencing. In short, this is a Runtime Type Identification (RTTI) method available to all objects
 MemberwiseClone()  This method exists to return a member-by-member copy of the current object, which is often used when cloning an object

The example of Object with empty class
 
// Remember! Person extends Object. 
class Person {} 

class Program 
{ 
  static void Main(string[] args) 
  { 
    Console.WriteLine("***** Fun with System.Object *****\n"); 
    Person p1 = new Person(); 
 
    // Use inherited members of System.Object. 
    Console.WriteLine("ToString: {0}", p1.ToString()); 
    Console.WriteLine("Hash code: {0}", p1.GetHashCode()); 
    Console.WriteLine("Type: {0}", p1.GetType());
    // Make some other references to p1. 
    Person p2 = p1; 
    object o = p2; 
 
    // Are the references pointing to the same object in memory? 
    if (o.Equals(p1) && p2.Equals(o)) 
    { 
      Console.WriteLine("Same instance!"); 
    } 
    Console.ReadLine(); 
  } 
} 

Testing Same value with different references
static void Main(string[] args) 
{ 
  Console.WriteLine("***** Fun with System.Object *****\n"); 
 
  // NOTE:  We want these to be identical to test 
  // the Equals() and GetHashCode() methods. 
  Person p1 = new Person("Homer", "Simpson", 50); 
  Person p2 = new Person("Homer", "Simpson", 50); 
 
  // Get stringified version of objects. 
  Console.WriteLine("p1.ToString() = {0}", p1.ToString()); 
  Console.WriteLine("p2.ToString() = {0}", p2.ToString()); 
 
  // Test Overridden Equals() 
  Console.WriteLine("p1 = p2?: {0}", p1.Equals(p2)); 
 
  // Test hash codes. 
  Console.WriteLine("Same hash codes?: {0}", p1.GetHashCode() == p2.GetHashCode()); 
  Console.WriteLine(); 
 
  // Change age of p2 and test again. 
  p2.Age = 45; 
  Console.WriteLine("p1.ToString() = {0}", p1.ToString()); 
  Console.WriteLine("p2.ToString() = {0}", p2.ToString()); 
  Console.WriteLine("p1 = p2?: {0}", p1.Equals(p2)); 
  Console.WriteLine("Same hash codes?: {0}", p1.GetHashCode() == p2.GetHashCode()); 
  Console.ReadLine(); 
} 
The Results
***** Fun with System.Object ***** 
 
p1.ToString() = [First Name: Homer; Last Name: Simpson; Age: 50] 
p2.ToString() = [First Name: Homer; Last Name: Simpson; Age: 50] 
p1 = p2?: True 
Same hash codes?: True 
 
p1.ToString() = [First Name: Homer; Last Name: Simpson; Age: 50] 
p2.ToString() = [First Name: Homer; Last Name: Simpson; Age: 45] 
p1 = p2?: False 
Same hash codes?: False