C#中List的Find方法的使用
发布日期:2021-11-07 06:40:49 浏览次数:7 分类:技术文章

本文共 1322 字,大约阅读时间需要 4 分钟。

查找List中的某个值,可以使用循环遍历对比,查找出结果。C#中提供了Find方法,可以直接使用,只要查找条件传入就可。如下:

class Program    {        public static void Main(string[] args)        {            List
userCollection = new List
(); 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 == 0; return user.UserID == 1 && user.UserName.Equals("testOne"); }); Console.WriteLine(resultUser != null ? resultUser.UserID + System.Environment.NewLine + resultUser.UserName : "没有查找到"); Console.ReadLine(); } } public class User { private int userID = 0; private string userName = string.Empty; public int UserID { get { return this.userID; } } public string UserName { get { return this.userName; } } public User(int userID, string userName) { this.userID = userID; this.userName = userName; } }

结果:

转载地址:https://blog.csdn.net/yysyangyangyangshan/article/details/7770783 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C#基础知识整理:基础知识(11) 值类型,引用类型
下一篇:C#基础知识整理:基础知识(10) 静态

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月07日 08时22分50秒