case Choices.ADD_PERSON:
Console.WriteLine("enter info for person to add.");
InfoForPerson();
PersonList Z = x + p;
break;
case Choices.REMOVE_PERSON:
Console.WriteLine("enter info for person to remove: ");
InfoForPerson();
Z = x - p;
break;
以上是从菜单中选择选项时发生的两件事。因为Choices.ADD_PERSON
结果符合预期,又加了一个人。但是,我假设 + & - 会以完全相同的方式起作用,只是相反,但它并没有发生。
public static PersonList operator -(PersonList x, Person y)
{
PersonList temp = x;
if (temp._Plist.Contains(y))
{
temp._Plist.Remove(y);
}
return temp; }
以上是我对减法运算符的定义。下面是我用来允许用户选择要添加/减去的人的代码。
public static void InfoForPerson()
{
Console.Write("Enter your name: ");
string name = Console.ReadLine();
string phone = ValidPhone();
string email = ValidEmail();
p = new Person(name, phone, email);
它适用于加法,而不是减法。我看了 p 并且它保存的数据很好,但它与列表中已经存在的项目不匹配。