C# 从列表c中删除自定义对象#

C# 从列表c中删除自定义对象#,c#,C#,我有这个密码 Class location { int X{Get; Set;} int Y{Get;Set;} } List<location> mylst = new List<location>(); Public Void SetupList() { for(int i =0; i<8; i++) { for(int b=0; b<8; b++) { location

我有这个密码

Class location
{
     int X{Get; Set;}
     int Y{Get;Set;}
}

List<location> mylst = new List<location>();
Public Void SetupList()
{
    for(int i =0; i<8; i++)
    {
       for(int b=0; b<8; b++)
       {
         location loc = new location();
         loc.x = b;
         loc.y = i;
          mylst.Add(loc);
       }
    }
}
但是我完全不知道该怎么做

任何想法都会有帮助,因为我尝试过的东西都不起作用

您有两种选择:

  • 。这样,当两个实例的值匹配时,
    loc==tofind
    将产生true

  • 通过显式比较值进行搜索:

    var listFound = myList.Where(loc => loc.X == tofind.X && loc.Y == tofind.Y).ToList();
    

  • 覆盖
    =
    运算符或
    等于()函数(并使用它),现在您正在检查指针是否相等。我如何覆盖它?我是列表新手,您可能想努力使您发布的代码能够真正编译…代码是可以计算出来的片段,我正在尝试进行更好的复制:
    
    var listFound = myList.Where(loc => loc.X == tofind.X && loc.Y == tofind.Y).ToList();