猿问

在vs2005如何改写当前代码,代码如何

  List<ChatInfo> ChatInfoArray = null;
            FindClass Fc = new FindClass();
            Fc.UserId = ID;
            ChatInfoArray = ListClass.Engin_ChatList.FindAll(Fc.PredicateChat);
            foreach (ChatInfo Cif in ChatInfoArray)
            {
                ListClass.Engin_ChatList.Remove(Cif);
            }
            return ChatInfoArray;

我改写的:

ArrayList ChatInfoArray = null ;
   FindClass Fc = new FindClass();
   Fc.UserId = ID;
   ChatInfoArray = ListClass.Engin_ChatList;
   foreach(ChatInfo Cif in ChatInfoArray)
   {
    if(!Fc.PredicateChat(Cif))
    {
     ListClass.Engin_ChatList.Remove(Cif);
    }
   }
   return ChatInfoArray ;


烙印99
浏览 387回答 2
2回答

不负相思意

假如你的LIST的ITEM是唯一的话,第一段代码有些多余。你改的第二段效率相对于第一段来说有提高效率了,省去了之前的 FindAll 。还有你的第二段为什么要 ArrayList ChatInfoArray = null ; 而不直接 ArrayList ChatInfoArray = ListClass.Engin_ChatList; 呢

白衣染霜花

如果你要改变某个集合,就不可以用foreach语句。否则会抛出异常,告诉你集合发生改变。应该用for语句。另外,linq是最方便的方式。FindClass Fc = new FindClass();Fc.UserId = ID;return from chatInfor in ChatInfoArray where Fc.PredicateChat(Cif) select chatInfor;
随时随地看视频慕课网APP
我要回答