如何名单< T> .IndexOf()上执行自定义对象的比较? [英] How does List<T>.IndexOf() perform comparisons on custom objects?

查看:136
本文介绍了如何名单< T> .IndexOf()上执行自定义对象的比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个类帐户对象和保持静态列表< T> 这些帐户对象。我的节目遍历列表中的每个帐户,与该帐户进行了一些工作,然后在顶部复位当它到达列表末尾。



我的问题是,我需要能够到帐户重新插入列表我的程序完成它的工作后,与一些更新的信息补充。我能做到这一点,如下写的,使用的IndexOf()函数中的静态列表来检查对象还是会失败,因为我添加的数据呢?我不理解哪些字段它比较,看是否两个对象是相同的。



注:没有允许重复在列表中,所以没有更新了错误的项目



<$ P $的风险p> 公共类帐户
{
公共字符串名称;
公共字符串密码;
公共字符串newInfo;
}

公共静态类资源
{
私人静态列表<&帐户GT;为accountList =新的List<&帐户GT;();
私有静态诠释currentAccountIndex = 0;

公共静态账户GetNextAccount()
{
如果(currentAccountIndex> AccountList.Count)
currentAccountIndex = 0;
收益为accountList [currentAccountIndex ++];
}

公共静态无效UpdateAccount(客户帐户)
{
INT指数;
如果((指数= AccountList.IndexOf(账户))> = 0)
为accountList [指数] =账户;
}
}

公共类节目
{
公共无效PerformWork()
{
帐户的帐户= Resources.GetNextAccount ();
//做一些工作
account.newInfo =富;
Resources.UpdateAccount(账户);
}
}


解决方案

您对象应该实现 IEquatable 接口和重写 。等于

 公共类帐户:IEquatable<&帐户GT; 
{
公共字符串名称;
公共字符串密码;
公共字符串newInfo;

公共布尔等于(帐户等)
{
//选择要考虑的是平等的帐户对象
//例如,假设newInfo什么是你想要什么考虑匹配
//(不分情况)
如果(其他== NULL)
返回false;

返回String.Equals(this.newInfo,other.newInfo,
StringComparison.OrdinalIgnoreCase);
}
}


I wrote a class of account objects and hold a static List<T> of those account objects. My program loops through each account in the list, performing some work with the account, and then resetting at the top when it reaches the end of the list.

My issue is that I need to be able to reinsert the account into the list after my program finishes working with it, with some updated info added. Can I do this as written below, using the IndexOf() function to check for the object in the static list or will it fail because I added data to it? I don't understand which fields it compares to see if the two objects are the same.

Note: no duplicates are allowed in the list so there is no risk of updating the wrong item

public class Account
{
   public string name;
   public string password;
   public string newInfo;
}

public static class Resources
{
   private static List<Account> AccountList = new List<Account>();
   private static int currentAccountIndex = 0;

   public static Account GetNextAccount()
   {
      if (currentAccountIndex > AccountList.Count)
         currentAccountIndex = 0;
      return AccountList[currentAccountIndex++];
   }

   public static void UpdateAccount(Account account)
   {
      int index;
      if ((index = AccountList.IndexOf(account)) >= 0)
         AccountList[index] = account;
   }
}

public class Program
{
   public void PerformWork()
   {
      Account account = Resources.GetNextAccount();
      // Do some work
      account.newInfo = "foo";
      Resources.UpdateAccount(account);
   }
}

解决方案

Your object should implement the IEquatable interface and override the Equals method.

public class Account : IEquatable<Account>
{
    public string name;
    public string password;
    public string newInfo;

    public bool Equals(Account other)
    {
       //Choose what you want to consider as "equal" between Account objects  
       //for example, assuming newInfo is what you want to consider a match
       //(regardless of case)
       if (other == null) 
             return false;

       return String.Equals(this.newInfo, other.newInfo, 
                           StringComparison.OrdinalIgnoreCase);
    }
}

这篇关于如何名单&LT; T&GT; .IndexOf()上执行自定义对象的比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆