请选择 进入手机版 | 继续访问电脑版
MSIPO技术圈 首页 IT技术 查看内容

C#(五十八)之C#List

2023-07-13

前几天,看同事写的代码中有list相关的字眼,百度了一下,原来是C#中list泛型集合。

了解一下。

List:泛型集合,List<T>类是 ArrayList 类的泛型等效类。该类使用大小可按需动态增加的数组实现 IList<T> 泛型接口。 

泛型的好处: 它为使用c#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类型转换,所以性能得到提高。 

使用命名空间:

<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#f92672">using</span> <span style="color:#f92672">System</span>.Collections.Generic;</span></span>

list一些方法和属性:

1.      Capacity 用于获取或设置List可容纳元素的数量。当数量超过容量时,这个值会自动增长。您可以设置这个值以减少容量,也可以调用trin()方法来减少容量以适合实际的元素数目。

2.      Count 属性,用于获取数组中当前元素数量

3.      Item( ) 通过指定索引获取或设置元素。对于List类来说,它是一个索引器。

4.      Add( ) 在List中添加一个对象的公有方法

5.      AddRange( ) 公有方法,在List尾部添加实现了ICollection接口的多个元素

6.      BinarySearch( ) 重载的公有方法,用于在排序的List内使用二分查找来定位指定元素.

7.      Clear( ) 在List内移除所有元素

8.      Contains( ) 测试一个元素是否在List内

9.      CopyTo( ) 重载的公有方法,把一个List拷贝到一维数组内

10.    Exists( ) 测试一个元素是否在List内

11.    Find( ) 查找并返回List内的出现的第一个匹配元素

12.    FindAll( ) 查找并返回List内的所有匹配元素

13.    GetEnumerator( ) 重载的公有方法,返回一个用于迭代List的枚举器

14.    Getrange( ) 拷贝指定范围的元素到新的List内

15.    IndexOf( ) 重载的公有方法,查找并返回每一个匹配元素的索引

16.    Insert( ) 在List内插入一个元素

17.    InsertRange( ) 在List内插入一组元素

18.    LastIndexOf( ) 重载的公有方法,,查找并返回最后一个匹配元素的索引

19.    Remove( ) 移除与指定元素匹配的第一个元素

20.    RemoveAt( ) 移除指定索引的元素

21.    RemoveRange( ) 移除指定范围的元素

22.    Reverse( ) 反转List内元素的顺序

23.    Sort( ) 对List内的元素进行排序

24.    ToArray( ) 把List内的元素拷贝到一个新的数组内

25.    trimToSize( ) 将容量设置为List中元素的实际数目

 

1:声明list

①、List<T> mList = new List<T>();  
T为列表中元素类型,现在以string类型作为例子

<span style="background-color:#282c34"><span style="color:#abb2bf">List<<span style="color:#e6c07b">string</span>> mList = <span style="color:#f92672">new</span> List<<span style="color:#e6c07b">string</span>>();</span></span>

②、List<T> testList =new List<T> (IEnumerable<T> collection);

以一个集合作为参数创建List:

<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#e6c07b">string</span>[] arr = { <span style="color:#98c379">"Ha"</span>, <span style="color:#98c379">"Hunter"</span>, <span style="color:#98c379">"Tom"</span>, <span style="color:#98c379">"Lily"</span>, <span style="color:#98c379">"Jay"</span>, <span style="color:#98c379">"Jim"</span>, <span style="color:#98c379">"Kuku"</span>, <span style="color:#98c379">"Locu"</span> };
List<<span style="color:#e6c07b">string</span>> mList = <span style="color:#f92672">new</span> List<<span style="color:#e6c07b">string</span>>(arr);</span></span>

2:添加元素 Add

添加一个元素

语法: List. Add(T item)  

<span style="background-color:#282c34"><span style="color:#abb2bf">mList.<span style="color:#e2b93d">Add(<span style="color:#9ccc65">"cmailla"</span>)</span>;
foreach (<span style="color:#e6c07b">string</span> item <span style="color:#f92672">in</span> mList)
{
      Console.<span style="color:#e2b93d">WriteLine(item)</span>;
}</span></span>

3:指定位置添加元素 Insert

语法: Insert(int index, T item); 

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList.Insert(<span style="color:#d19a66">0</span>,<span style="color:#98c379">"qwert"</span>);
            foreach (<span style="color:#e6c07b">string</span> <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> mList)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

4:添加元素集合 AddRange

语法: List. AddRange(IEnumerable<T> collection)   

<span style="background-color:#282c34"><span style="color:#abb2bf">            <span style="color:#e6c07b">string</span>[] dfg = { <span style="color:#98c379">"hiocvhi"</span>,<span style="color:#98c379">"jioosdfhio"</span>,<span style="color:#98c379">"erw"</span>};
            mList.AddRange(dfg);
            foreach (<span style="color:#e6c07b">string</span> <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> mList)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

5:删除指定元素

语法:List. Remove(T item)

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList.Remove(<span style="color:#98c379">"erw"</span>);
            foreach (<span style="color:#e6c07b">string</span> <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> mList)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

6:删除指定下标元素

语法:List. RemoveAt(int index);   

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList.RemoveAt(<span style="color:#d19a66">3</span>);
            foreach (<span style="color:#e6c07b">string</span> <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> mList)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

7:从下标index开始,删除count个元素

语法:List. RemoveRange(int index, int count);

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList.RemoveRange(<span style="color:#d19a66">3</span>,<span style="color:#d19a66">5</span>);
            foreach (<span style="color:#e6c07b">string</span> <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> mList)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

8:判断某个元素是否在该List中:

语法:List. Contains(T item)   返回值为:true/false

<span style="background-color:#282c34"><span style="color:#abb2bf">            bool s = mList.Contains(<span style="color:#98c379">"Ha"</span>);
            Console.WriteLine(s);
            Console.WriteLine(<span style="color:#98c379">"</span>
<span style="color:#98c379">");</span></span></span>

9:给List里面元素排序:

语法: List. Sort ()   默认是元素第一个字母按升序

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList<span style="color:#d19a66">.Sort</span>();
            foreach (var item in mList)
            {
                Console<span style="color:#d19a66">.WriteLine</span>(item);
            }
            Console<span style="color:#d19a66">.WriteLine</span>("
");</span></span>

10:给List里面元素顺序反转:

语法: List. Reverse ()   可以与List. Sort ()配合使用,达到想要的效果

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList<span style="color:#d19a66">.Reverse</span>();
            foreach (var item in mList)
            {
                Console<span style="color:#d19a66">.WriteLine</span>(item);
            }
            Console<span style="color:#d19a66">.WriteLine</span>("
");</span></span>

11:List清空:

语法:List. Clear () 

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList<span style="color:#d19a66">.Clear</span>();
            foreach (var item in mList)
            {
                Console<span style="color:#d19a66">.WriteLine</span>(item);
            }
            Console<span style="color:#d19a66">.WriteLine</span>("
");</span></span>

12:ist.FindAll方法:检索与指定谓词所定义的条件相匹配的所有元素 

语法:public List<T> FindAll(Predicate<T> match);

Predicate是对方法的委托,如果传递给它的对象与委托中定义的条件匹配,则该方法返回 true。当前 List 的元素被逐个传递给Predicate委托,并在 List 中向前移动,从第一个元素开始,到最后一个元素结束。当找到匹配项时处理即停止。

Predicate 可以委托给一个函数或者一个拉姆达表达式:

正常委托:

<span style="background-color:#282c34"><span style="color:#abb2bf">        List<<span style="color:#e6c07b">string</span>> subList = mList.FindAll(ListFind); <span style="color:#b18eb1"><em>//委托给ListFind函数</em></span>
        <span style="color:#b18eb1"><em>//ListFind函数</em></span>
        <span style="color:#61aeee"><span style="color:#f92672">public</span> <span style="color:#f92672">static</span> <span style="color:#e6c07b">bool</span> <span style="color:#61aeee">ListFind</span>(<span style="color:#a6e22e"><span style="color:#e6c07b">string</span> name</span>)</span>
        {
            Console.WriteLine(name);
            <span style="color:#f92672">if</span> (name.Length > <span style="color:#d19a66">3</span>)
            {
                <span style="color:#f92672">return</span> <span style="color:#56b6c2">true</span>;
            }
            <span style="color:#f92672">return</span> <span style="color:#56b6c2">false</span>;
        }</span></span>

匿名委托:

<span style="background-color:#282c34"><span style="color:#abb2bf">List<<span style="color:#e6c07b">string</span>> subList = mList.<span style="color:#e2b93d">FindAll( delegate(string name)</span> {
     Console.<span style="color:#e2b93d">WriteLine(name)</span>;
     <span style="color:#f92672">if</span> (name.Length > <span style="color:#d19a66">3</span>)
     {
           return <span style="color:#56b6c2">true</span>;
      }
       return <span style="color:#56b6c2">false</span>;
});</span></span>

Lambda表达式:

<span style="background-color:#282c34"><span style="color:#abb2bf">List<string> subList = mList.FindAll(<span style="color:#61aeee"><span style="color:#a6e22e">name</span> =></span> {
      <span style="color:#e6c07b">Console</span>.WriteLine(name);
      <span style="color:#f92672">if</span> (name.<span style="color:#e6c07b">Length</span> > <span style="color:#d19a66">3</span>)
      {
            <span style="color:#f92672">return</span> <span style="color:#56b6c2">true</span>;
      }
      <span style="color:#f92672">return</span> <span style="color:#56b6c2">false</span>;
});</span></span>

13:List.Find 方法:搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List 中的第一个匹配元素。 

语法:public T Find(Predicate<T> match);

<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#e6c07b">string</span> listFind1 = mList.<span style="color:#e2b93d">Find(ListFind)</span>;  <span style="color:#b18eb1"><em>//委托给ListFind函数</em></span>
Console.<span style="color:#e2b93d">WriteLine(listFind1)</span>;</span></span>

14:List.FindLast 方法:搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List 中的最后一个匹配元素。

语法:public T FindLast(Predicate<T> match);

<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#e6c07b">string</span> listFind2 = mList.<span style="color:#e2b93d">FindLast(ListFind)</span>;  <span style="color:#b18eb1"><em>//委托给ListFind函数</em></span>
Console.<span style="color:#e2b93d">WriteLine(listFind2)</span>;</span></span>

15:List.TrueForAll方法:确定是否 List 中的每个元素都与指定的谓词所定义的条件相匹配。

语法:public bool TrueForAll(Predicate<T> match);

<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#e6c07b">bool</span> flag = mList.<span style="color:#e2b93d">TrueForAll(delegate(string name)</span>
{
      Console.<span style="color:#e2b93d">WriteLine(name)</span>;
      <span style="color:#f92672">if</span> (name.Length > <span style="color:#d19a66">3</span>)
      {
           return <span style="color:#56b6c2">true</span>;
      }
      return <span style="color:#56b6c2">false</span>;
});</span></span>

16:List.Take(n)方法:  获得前n行 返回值为IEnumetable<T>,T的类型与List<T>的类型一样:就是复制

<span style="background-color:#282c34"><span style="color:#abb2bf">            IEnumerable<<span style="color:#e6c07b">string</span>> mmmlist = mList.Take(<span style="color:#d19a66">3</span>);
            foreach (<span style="color:#e6c07b">string</span> <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> mmmlist)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

17:List.Where方法:检索与指定谓词所定义的条件相匹配的所有元素。跟List.FindAll方法类似。

使用该方法定义存储集合的元素开头是:IEnumerable

<span style="background-color:#282c34"><span style="color:#abb2bf">            IEnumerable<<span style="color:#e6c07b">string</span>> whereList = mList.Where(ListFind);
            foreach (var <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> whereList)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

18:List.RemoveAll方法:移除与指定的谓词所定义的条件相匹配的所有元素。

语法: public int RemoveAll(Predicate<T> match);

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList.RemoveAll<span style="color:#e6c07b">(name</span> => {
                <span style="color:#f92672">if</span> <span style="color:#e6c07b">(name</span>.Length <= <span style="color:#d19a66">6</span>)
                {
                    <span style="color:#f92672">return</span><span style="color:#e6c07b"> true</span>;
                }
                <span style="color:#f92672">else</span> {
                    <span style="color:#f92672">return</span><span style="color:#e6c07b"> false</span>;
                }
            });
            foreach (var <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> mList)
            {
                Console.WriteLine(<span style="color:#d19a66">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

测试使用全部代码:

<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#d19a66">using</span> <span style="color:#e6c07b">System</span>;
<span style="color:#d19a66">using</span> <span style="color:#e6c07b">System</span>.Collections.Generic;
<span style="color:#d19a66">using</span> <span style="color:#e6c07b">System</span>.Linq;
<span style="color:#d19a66">using</span> <span style="color:#e6c07b">System</span>.Text;
<span style="color:#d19a66">using</span> <span style="color:#e6c07b">System</span>.Threading.Tasks;
 
<span style="color:#d19a66">namespace</span> <span style="color:#61aeee">listys</span>
{
    <span style="color:#f92672">class</span> <span style="color:#e6c07b">Program</span>
    {
        <span style="color:#f92672">static</span> <span style="color:#d19a66">void</span> <span style="color:#e6c07b">Main</span>(<span style="color:#d19a66">string</span>[] <span style="color:#d19a66">args</span>)
        {
            <span style="color:#b18eb1"><em>// 声明列表</em></span>
            <span style="color:#b18eb1"><em>// List<string> mList = new List<string>();</em></span>
 
            <span style="color:#d19a66">string</span>[] <span style="color:#d19a66">arr</span> <span style="color:#f92672">=</span> { <span style="color:#98c379">"Ha"</span>, <span style="color:#98c379">"Hunter"</span>, <span style="color:#98c379">"Tom"</span>, <span style="color:#98c379">"Lily"</span>, <span style="color:#98c379">"Jay"</span>, <span style="color:#98c379">"Jim"</span>, <span style="color:#98c379">"Kuku"</span>, <span style="color:#98c379">"Locu"</span> };
            <span style="color:#e6c07b">List</span><span style="color:#f92672"><</span><span style="color:#d19a66">string</span><span style="color:#f92672">></span> <span style="color:#d19a66">mList</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">new</span> <span style="color:#e6c07b">List</span><span style="color:#f92672"><</span><span style="color:#d19a66">string</span><span style="color:#f92672">></span>(<span style="color:#d19a66">arr</span>);
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">mList</span>.Count);
 
            <span style="color:#d19a66">mList</span>.Add(<span style="color:#98c379">"cmailla"</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">mList</span>.Insert(<span style="color:#d19a66">0</span>,<span style="color:#98c379">"qwert"</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">string</span>[] <span style="color:#d19a66">dfg</span> <span style="color:#f92672">=</span> { <span style="color:#98c379">"hiocvhi"</span>,<span style="color:#98c379">"jioosdfhio"</span>,<span style="color:#98c379">"erw"</span>};
            <span style="color:#d19a66">mList</span>.AddRange(<span style="color:#d19a66">dfg</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">mList</span>.Remove(<span style="color:#98c379">"erw"</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">mList</span>.RemoveAt(<span style="color:#d19a66">3</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">mList</span>.RemoveRange(<span style="color:#d19a66">3</span>,<span style="color:#d19a66">5</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">bool</span> <span style="color:#d19a66">s</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.Contains(<span style="color:#98c379">"Ha"</span>);
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">s</span>);
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">mList</span>.Sort();
            <span style="color:#61aeee">foreach</span> (<span style="color:#f92672">var</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">mList</span>.Reverse();
            <span style="color:#61aeee">foreach</span> (<span style="color:#f92672">var</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#b18eb1"><em>/*mList.Clear();
            foreach (var item in mList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("
");//*/</em></span>
 
            <span style="color:#b18eb1"><em>// 正常委托</em></span>
            <span style="color:#b18eb1"><em>// List<string> subList = mList.FindAll(ListFind); //委托给ListFind函数</em></span>
            <span style="color:#b18eb1"><em>// 匿名委托</em></span>
            <span style="color:#b18eb1"><em>/*List<string> subList = mList.FindAll( delegate(string name) {
                Console.WriteLine(name);
                if (name.Length > 3)
                {
                    return true;
                }
                return false;
            });//*/</em></span>
 
            <span style="color:#e6c07b">List</span><span style="color:#f92672"><</span><span style="color:#d19a66">string</span><span style="color:#f92672">></span> <span style="color:#d19a66">subList</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.FindAll(<span style="color:#d19a66">name</span> <span style="color:#f92672">=</span><span style="color:#f92672">></span> {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">name</span>);
                <span style="color:#f92672">if</span> (<span style="color:#d19a66">name</span>.Length <span style="color:#f92672">></span> <span style="color:#d19a66">3</span>)
                {
                    <span style="color:#f92672">return</span> <span style="color:#56b6c2">true</span>;
                }
                <span style="color:#f92672">return</span> <span style="color:#56b6c2">false</span>;
            });
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">sss</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">subList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"element in subList: "</span> <span style="color:#f92672">+</span> <span style="color:#d19a66">sss</span>);
            }
 
            <span style="color:#d19a66">string</span> <span style="color:#d19a66">listFind1</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.Find(<span style="color:#e6c07b">ListFind</span>);  <span style="color:#b18eb1"><em>//委托给ListFind函数</em></span>
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">listFind1</span>);
 
            <span style="color:#d19a66">string</span> <span style="color:#d19a66">listFind2</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.FindLast(<span style="color:#e6c07b">ListFind</span>);  <span style="color:#b18eb1"><em>//委托给ListFind函数</em></span>
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">listFind2</span>);
 
            <span style="color:#d19a66">bool</span> <span style="color:#d19a66">flag</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.TrueForAll(<span style="color:#61aeee">delegate</span>(string name)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">name</span>);
                <span style="color:#f92672">if</span> (<span style="color:#d19a66">name</span>.Length <span style="color:#f92672">></span> <span style="color:#d19a66">3</span>)
                {
                    <span style="color:#f92672">return</span> <span style="color:#56b6c2">true</span>;
                }
                <span style="color:#f92672">return</span> <span style="color:#56b6c2">false</span>;
            });
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">flag</span>);
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
 
            <span style="color:#e6c07b">IEnumerable</span><span style="color:#f92672"><</span><span style="color:#d19a66">string</span><span style="color:#f92672">></span> <span style="color:#d19a66">mmmlist</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.Take(<span style="color:#d19a66">3</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mmmlist</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
 
            <span style="color:#e6c07b">IEnumerable</span><span style="color:#f92672"><</span><span style="color:#d19a66">string</span><span style="color:#f92672">></span> <span style="color:#d19a66">whereList</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.Where(<span style="color:#e6c07b">ListFind</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#f92672">var</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">whereList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
 
            <span style="color:#d19a66">mList</span>.RemoveAll(<span style="color:#d19a66">name</span> <span style="color:#f92672">=</span><span style="color:#f92672">></span> {
                <span style="color:#f92672">if</span> (<span style="color:#d19a66">name</span>.Length <span style="color:#f92672"><=</span> <span style="color:#d19a66">6</span>)
                {
                    <span style="color:#f92672">return</span> <span style="color:#56b6c2">true</span>;
                }
                <span style="color:#f92672">else</span> {
                    <span style="color:#f92672">return</span> <span style="color:#56b6c2">false</span>;
                }
            });
            <span style="color:#61aeee">foreach</span> (<span style="color:#f92672">var</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
            <span style="color:#e6c07b">Console</span>.ReadLine();
        }
        <span style="color:#b18eb1"><em>//ListFind函数</em></span>
        <span style="color:#d19a66">public</span> <span style="color:#f92672">static</span> <span style="color:#d19a66">bool</span> <span style="color:#e6c07b">ListFind</span>(<span style="color:#d19a66">string</span> <span style="color:#d19a66">name</span>)
        {
            <span style="color:#f92672">if</span> (<span style="color:#d19a66">name</span>.Length <span style="color:#f92672">></span> <span style="color:#d19a66">3</span>)
            {
                <span style="color:#f92672">return</span> <span style="color:#56b6c2">true</span>;
            }
            <span style="color:#f92672">return</span> <span style="color:#56b6c2">false</span>;
        }
    }
}</span></span>

有好的建议,请在下方输入你的评论。

 

相关阅读

手机版|MSIPO技术圈 皖ICP备19022944号-2

Copyright © 2024, msipo.com

返回顶部