using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Crystal_Message
{
class Person
{
private string firstName ="";
private string lastName= "";
private string phone="";
public Person(string firstName, string lastName, string phone)
{
this.FirstName = firstName;
this.LastName = lastName;
this.PhoneNumber = phone;
}
public string FirstName
{
get { return firstName; }
private set
{
if (string.IsNullOrWhiteSpace(value)){
throw new ArgumentNullException("Must Include First Name");
}
this.firstName = value;
}
}
public string LastName
{
get { return lastName; }
private set
{
if (string.IsNullOrWhiteSpace(value)){
throw new ArgumentNullException("Must Include Last Name");
}
this.lastName = value;
}
}
public string PhoneNumber
{
get { return phone; }
private set
{
if (string.IsNullOrWhiteSpace(value)){
throw new ArgumentNullException("Must Include Phone Number");
}
this.phone = value;
}
}
public override string ToString()
{
return "First Name: " + this.FirstName + " " + " Last Name: " + this.LastName + " " + " Phone Number: " + this.PhoneNumber;
}
public override bool Equals(object obj)
{
if(obj == null)
{
return false;
}
Person testEquals = obj as Person;
if((System.Object)testEquals == null)
{
return false;
}
return (this.firstName == testEquals.firstName) && (this.lastName == testEquals.lastName) && (this.phone == testEquals.phone);
}
/*
public override int GetHashCode()
{
return
}
*/
}
}
我一直在遵循MSDN的指导方针。两个问题:
- 我是否正确实现了equals方法?
- 有谁能为我的类正确地实现GetHashCode?MSDN使用x ^ y,但我不能这样做。