简体   繁体   English

C#运算符重载用法

[英]c# operator overloading usage

public static ListOfPeople operator +( ListOfPeople x, Person y)
    {
        ListOfPeople temp = new ListOfPeople(x);
        if(!temp.PeopleList.Contains(y))
        {
            temp.PeopleList.Add(y);
        }
        temp.SaveNeeded = true;
        return temp;
    }

So, I've never used the overload functionality of operators, and I'm trying to make sense of how to add objects from my class (Person) to my Collection class (ListOfPeople). 因此,我从未使用过运算符的重载功能,并且试图弄清如何将类(人)中的对象添加到我的Collection类(ListOfPeople)中。

ListOfPeople contains an attribute List<Person> PeopleList ; ListOfPeople包含一个属性List<Person> PeopleList

My difficulty is in how to get a pre-existing List inside of this method to add a new Person to. 我的困难在于如何在此方法中获取预先存在的列表以向其中添加新的Person。 ListOfPeople temp = new ListOfPeople(x);

I have an error on this line because I have no constructor that takes a ListOfPeople argument. 我在这行上有一个错误,因为我没有构造函数接受ListOfPeople参数。 If I were to make it ListOfPeople temp = new ListOfPeople(); 如果我将其ListOfPeople temp = new ListOfPeople(); then Temp would just call my default constructor where I simply create a new, empty list, and that doesn't allow me to add to a pre-existing list either. 然后Temp会调用我的默认构造函数,在该构造函数中我只是创建一个新的空列表,而这也不允许我添加到预先存在的列表中。

I'm just not sure how I get 'temp' to actually reference my pre-existing list. 我只是不确定如何获得“ temp”来实际引用我先前存在的列表。

Use as follows: 用法如下:

public static ListOfPeople operator +( ListOfPeople x, Person y)
{
    ListOfPeople temp = x;
    if(!temp.PeopleList.Contains(y))
    {
        temp.PeopleList.Add(y);
    }
    temp.SaveNeeded = true;
    return temp;
}

public static ListOfPeople operator +( Person y, ListOfPeople x)
{
    ListOfPeople temp = x;
    if(!temp.PeopleList.Contains(y))
    {
        temp.PeopleList.Add(y);
    }
    temp.SaveNeeded = true;
    return temp;
}
  • 1st allows you to use: list = list + person 第一种允许您使用: list = list + person
  • 2nd allows you to use: list = person + list 第二个允许您使用: list = person + list

You may also want to overload += operator (non-static) so that you can use list += person 您可能还想重载+=运算符(非静态),以便可以使用list += person

EDIT 编辑

Though I solved the problem mentioned. 虽然我解决了提到的问题。 But, then, I agree with others on the operands of '+' being immutable. 但是,然后,我同意其他观点,认为“ +”的操作数是不变的。

Below is update to existing code (assuming ListOfPeople.PeopleList is List<Person> ): 以下是对现有代码的更新(假设ListOfPeople.PeopleList is List<Person> ):

public static ListOfPeople operator +( ListOfPeople x, Person y)
{
    ListOfPeople temp = new ListOfPeople();
    temp.PeopleList.addRange(x);
    if(!temp.PeopleList.Contains(y))
    {
        temp.PeopleList.Add(y);
    }
    temp.SaveNeeded = true;
    return temp;
}

public static ListOfPeople operator +( Person y, ListOfPeople x)
{
    ListOfPeople temp = new ListOfPeople();
    temp.PeopleList.addRange(x);
    if(!temp.PeopleList.Contains(y))
    {
        temp.PeopleList.Add(y);
    }
    temp.SaveNeeded = true;
    return temp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM