示例
Person.cs
public class Person
{
//properties
public string PersonName { get; set; }
public string Email { get; set; }
//overriding Equals method
public override bool Equals(object obj)
{
Person p = (Person)obj;
if (this.PersonName == p.PersonName && this.Email == p.Email)
{
return true;
}
else
{
return false;
}
}
//overriding ToString method
public override string ToString()
{
return "Person name: " + this.PersonName;
}
}
Program.cs
using System;
class Program
{
static void Main()
{
//create an object of Person class
System.Object obj = new Person() { PersonName = "Scott", Email = "scott@gmail.com" };
//access methods
Console.WriteLine(obj.Equals(new Person() { PersonName = "Scott", Email = "scott@gmail.com" }));
Console.WriteLine(obj.GetHashCode());
Console.WriteLine(obj.ToString());
Console.WriteLine(obj.GetType().ToString());
Console.ReadKey();
}
}