您的位置:首页 > 编程语言 > C#

C#使用Find方法

2014-10-24 17:56 239 查看
在List<T>,ArrayList等数据的集合类中,我们可以看到它包含一个Find方法。这个方法的结构如下:(以List<T>为例)

public T Find (

       Predicate<T> match

)

 

Find的参数是一个返回类型为bool的函数,参数为T

 

例如:

public Form1()

        {

            InitializeComponent();

            List<string> StringList = new List<string>();

            StringList.AddRange(new string[] { "abc", "def", "ghi" });

            MessageBox.Show(StringList.Find(EndWithI));

        }

 

        private bool EndWithI(string s)

        {

            if (s[s.Length - 1].ToString() == "i")

            {

                return true;

            }

            return false;

        }

 

这个函数的流程是:遍历每一个项,将项放入判断的函数,如果这个项满足条件,就返回该项(Find只返回第一个符合的项,FindAll返回满足条件的项的List)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# find