using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace list实验2
{
class Program
{
static void Main(string[] args)
{
string[] str = { "1111","22","333","wewewe"};
List<string> mList = new List<string>(str);
string listFind1 = mList.Find(ListFind); //委托给ListFind函
Console.WriteLine("查询的结果为:"+listFind1); //输出是wewewe
IEnumerable<string> whereList = mList.Where(name => //List.Where方法:检索与指定谓词所定义的条件相匹配的所有元素。跟List.FindAll方法类似。
{
if (name.Length > 3)
{
return true;
}
else
{
return false;
}
});
foreach (string s in whereList)
{
Console.WriteLine("element in whereList: " + s); //这时subList存储的就是所有长度大于3的元素
}
mList.RemoveAll(name => //List.RemoveAll方法:移除与指定的谓词所定义的条件相匹配的所有元素。
{
if (name.Length > 3)
{
return true; //这时mList存储的就是移除长度大于3之后的元素。
}
else
{
return false;
}
});
foreach (string s in mList)
{
Console.WriteLine("element in mList: " + s);
}
Console.ReadLine();
}
//ListFind函数:
public static bool ListFind(string name)
{
if (name.Length > 5)
{
return true;
}
return false;
}
}
}
list<>实验
最新推荐文章于 2021-01-29 11:12:04 发布