C# List 中 Find 方法

时间:2022-12-11 14:52:49

实例化一个集合

List<User> userCollection = new List<User>();
userCollection.Add(new User(1, "testOne"));
userCollection.Add(new User(2, "testTwo"));
userCollection.Add(new User(3, "testThree"));

方法一

User resultUser = userCollection.Find(
delegate(User user)
{
return user.UserID == 1 && user.UserName.Equals("testOne");
});

方法二

User resultUser = userCollection.Find((User user) =>user.UserID == 1 && user.UserName.Equals("testOne"));