Csharp/CSharp Tutorial/Class/System.Object
Содержание
- 1 Call object.Equals to compare objects
- 2 Methods of the object Class
- 3 Static members of System.Object: object.Equals
- 4 Static members of System.Object: object.ReferenceEquals
- 5 Use the inherited Object methods: Equals
- 6 Using Methods inherited from the System.Object class: ToString, GetType, GetHashCode, Equals, ReferenceEquals
Call object.Equals to compare objects
<source lang="csharp">public class MainClass {
static bool TestForEquality( object obj1, object obj2 ) { if( obj1 == null ) { return false; } return obj1.Equals( obj2 ); } static void Main() { object obj1 = new System.Object(); object obj2 = null; System.Console.WriteLine( "obj1 == obj2 is {0}", TestForEquality(obj2, obj1) ); }
}</source>
obj1 == obj2 is False
Methods of the object Class
Method Purpose public virtual bool Equals(object ob) whether the object is the same as the one referred to by ob. public static bool Equals(object ob1, object ob2) whether ob1 is the same as ob2. protected Finalize() Performs shutdown actions prior to garbage collection. public virtual int GetHashCode() Returns the hash code. public Type GetType() Return the type of an object. protected object MemberwiseClone() Makes a "shallow copy" of the object. (The members are copied, but objects referred to by members are not.) public static bool ReferenceEquals(object ob1, object ob2) whether ob1 and ob2 refer to the same object. public virtual string ToString() Returns a string that describes the object. It is automatically called when an object is output using WriteLine().
Static members of System.Object: object.Equals
<source lang="csharp">using System; using System.Text; class Person {
public Person(string fname, string lname, string ssn, byte a) { FirstName = fname; LastName = lname; SSN = ssn; age = a; } public string FirstName; public string LastName; public string SSN; public byte age; public override bool Equals(object o) { Person temp = (Person)o; if(temp.FirstName == this.FirstName && temp.LastName == this.LastName && temp.SSN == this.SSN && temp.age == this.age) { return true; } else return false; } public override string ToString() { StringBuilder sb = new StringBuilder(); sb.AppendFormat("[FirstName= {0}", this.FirstName); sb.AppendFormat(" LastName= {0}", this.LastName); sb.AppendFormat(" SSN= {0}", this.SSN); sb.AppendFormat(" Age= {0}]", this.age); return sb.ToString(); } public override int GetHashCode() { return SSN.GetHashCode(); }
}
class MainClass {
public static void Main(string[] args) { Person p1 = new Person("A", "B", "222-22-2222", 98); Person p2 = new Person("A", "B", "222-22-2222", 98); Console.WriteLine("P3 and P4 have same state: {0}", object.Equals(p1, p2)); }
}</source>
P3 and P4 have same state: True
Static members of System.Object: object.ReferenceEquals
<source lang="csharp">using System; using System.Text; class Person {
public Person(string fname, string lname, string ssn, byte a) { FirstName = fname; LastName = lname; SSN = ssn; age = a; } public string FirstName; public string LastName; public string SSN; public byte age; public override bool Equals(object o) { Person temp = (Person)o; if(temp.FirstName == this.FirstName && temp.LastName == this.LastName && temp.SSN == this.SSN && temp.age == this.age) { return true; } else return false; } public override string ToString() { StringBuilder sb = new StringBuilder(); sb.AppendFormat("[FirstName= {0}", this.FirstName); sb.AppendFormat(" LastName= {0}", this.LastName); sb.AppendFormat(" SSN= {0}", this.SSN); sb.AppendFormat(" Age= {0}]", this.age); return sb.ToString(); } public override int GetHashCode() { return SSN.GetHashCode(); }
}
class MainClass {
public static void Main(string[] args) { Person p1 = new Person("A", "B", "222-22-2222", 98); Person p2 = new Person("A", "B", "222-22-2222", 98); Console.WriteLine("P1 and P2 are pointing to same object: {0}", object.ReferenceEquals(p1, p2)); }
}</source>
P1 and P2 are pointing to same object: False
Use the inherited Object methods: Equals
<source lang="csharp">using System; class MainClass {
public static void Main(string[] args) { MainClass c1 = new MainClass(); MainClass c2 = c1; object o = c2; if(o.Equals(c1) && c2.Equals(o)) Console.WriteLine("Same instance!\n"); }
}</source>
Same instance!
Using Methods inherited from the System.Object class: ToString, GetType, GetHashCode, Equals, ReferenceEquals
<source lang="csharp">using System; public class Employee {
public string firstName; public string lastName; public Employee(string firstName, string lastName) { this.firstName = firstName; this.lastName = lastName; } public void Display() { Console.WriteLine("firstName = " + firstName); Console.WriteLine("lastName = " + lastName); } public static Employee Copy(Employee Employee) { return (Employee) Employee.MemberwiseClone(); }
}
class MainClass {
public static void Main() { Console.WriteLine("Creating Employee objects"); Employee myEmployee = new Employee("A", "M"); Employee myOtherEmployee = new Employee("B", "N"); Console.WriteLine("myEmployee details:"); myEmployee.Display(); Console.WriteLine("myOtherEmployee details:"); myOtherEmployee.Display();
Console.WriteLine("myEmployee.ToString() = " + myEmployee.ToString()); Console.WriteLine("myEmployee.GetType() = " + myEmployee.GetType()); Console.WriteLine("myEmployee.GetHashCode() = " + myEmployee.GetHashCode()); Console.WriteLine("Employee.Equals(myEmployee, myOtherEmployee) = " + Employee.Equals(myEmployee, myOtherEmployee)); Console.WriteLine("Employee.ReferenceEquals(myEmployee, myOtherEmployee) = " + Employee.ReferenceEquals(myEmployee, myOtherEmployee)); }
}</source>
Creating Employee objects myEmployee details: firstName = A lastName = M myOtherEmployee details: firstName = B lastName = N myEmployee.ToString() = Employee myEmployee.GetType() = Employee myEmployee.GetHashCode() = 58225482 Employee.Equals(myEmployee, myOtherEmployee) = False Employee.ReferenceEquals(myEmployee, myOtherEmployee) = False