狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C# 泛型List集合的定義、作用、用法

admin
2021年3月10日 11:26 本文熱度 4182
定義:List<T>類表示可通過索引訪問的對象的強類型列表,提供用于對列表進行搜索、排序和操作的方法。

作用:
泛型最常見的用途是泛型集合
我們在創(chuàng)建列表類時,列表項的數(shù)據(jù)類型可能是int,string或其它類型,如果對列表類的處理方法相同,就沒有必要事先指定數(shù)據(jù)類型,留待列表類實例化時再指定。相當(dāng)于把數(shù)據(jù)類型當(dāng)成參數(shù),這樣可以最大限度地重用代碼,保護類型的安全以及提高性能。

List的一般用法
所屬命名空間: System.Collections.Generic
public class List<T>:IList<T>,Icollection<T>,IEnumerable<T>,IList,Icollection,Ienumerable
List<T>是ArrayList類的泛型等效類,該類使用大小可按需動態(tài)增加的數(shù)組實現(xiàn)IList<T>泛型接口

(1)聲明 List<T>mlist = new List<T>();
 eg: string[] Arr = {"a","b","c"};
     List<string> mlist = new List<string>(Arr);

(2)添加一個元素 List.Add(T item) 
   eg: mlist.Add("d");

(3)添加集合元素
   eg: string[] Arr2 ={"f","g"."h"};
       mlist.AddRange(Arr2);

(4)在index位置添加一個元素 Insert(int index,T item)
   eg: mlist.Insert(1,"p");

(5)遍歷List中元素
  foreach(T element in mlist) T的類型與mlist聲明時一樣
   {
       Console.WriteLine(element);
          }
    eg:
    foreach(string s in mlist)
          {
             Console.WriteLine(s);
           }

(6)刪除元素
    List.Remove(T item) 刪除一個值
    eg: mlist.Remove("a");

    List.RemoveAt(int index);刪除下標(biāo)為index的元素
    eg: mlist.RemoveAt(0);
   
    List.RemoveRange(int index,int count); 下標(biāo)index開始,刪除count個元素
    eg:mlist.RemoveRange(3,2);

(7)判斷某個元素是否在該List中
    List.Contains(T item) 返回true或false
    eg:
    if(mlist.Contains"("g"))
       Console.WriteLine("g存在列表中");
    else
       mlist.Add("g");

(8)給List里面元素排序 List.Sort() 默認(rèn)是元素每一個字母按升序
   eg: mlist.Sort();

(9)給List里面元素順序反轉(zhuǎn) List.Reverse() 可以與List.Sort()配合使用

(10)List清空 List.Clear()
   eg: mlist.Clear();

(11)獲得List中元素數(shù)目 List.Count() 返回int值
   eg: mlist.count();

List進階,強大方法
(1)List.FindAll方法:檢索與指定謂詞所定義的條件相匹配的所有元素
    class program
    {
       static void Main(stirng[] args)
       {
         student stu = new student();
         stu.Name="arron";
         List<student> students= new List<student>();
         students.Add(stu);
         students.Add(new student("candy"));
         FindName myname = new FindName("arron");
         foreach(student s in students.FindAll(new Predicate<student>(myname.IsName)))
         { Console.WriteLine(s);}
       }

    public class student
    {
       public string Name{get;set;}
       public student(){}
       public override string ToString()
        {
            return string.Format("姓名:{0}",Name);
         }
     }

    public class FindName
    {
      private string _name;
      public FindName(string Name)
      {  this._name=Name;}
      public bool IsName(student s)
       { return (s.Name==_name)?true:false;}
    }

(2)List.Find方法 搜索與指定謂詞所定義的條件相匹配的元素,并返回整個List中的第一個匹配元素
  eg:
    //Predicate是對方法的委托,如果傳遞給它的對象與委托定義的條件匹配,則該方法返回true,當(dāng)前List的元素被逐個傳遞給Predicate委托,并在List中間前移動,從第一個元素開始,到最后一個元素結(jié)束,當(dāng)找到匹配項時處理停止

  第一種方法 委托給拉姆達(dá)表達(dá)式:
  eg:  
     string listFind = mlist.Find(name=>
       {
          if(name.length>3)
             return true;
          return false;
       });

    第二種方法 委托給一個函數(shù):
    eg:
     public bool ListFind(string name)
        {
            if (name.Length > 3)
            {
                return true;
            }
            return false;
        }
      這兩種方法的結(jié)果是一樣的

(3) List.FindLast方法  public T FindLast(Predicate<T> match);確定是否 List 中的每個元素都與指定的謂詞所定義的條件相匹配。用法與List.Find相同。

(4) List.TrueForAll方法:  確定是否 List 中的每個元素都與指定的謂詞所定義的條件相匹配。
 public bool TrueForAll(Predicate<T> match);

(5) List.Take(n):  獲得前n行 返回值為IEnumetable<T>,T的類型與List<T>的類型一樣
E.g.:
IEnumerable<string> takeList=  mList.Take(5);
          foreach (string s in takeList)
          {
              Console.WriteLine("element in takeList: " + s);
          }
       這時takeList存放的元素就是mList中的前5個

(6) List.Where方法:檢索與指定謂詞所定義的條件相匹配的所有元素。跟List.FindAll方法類似。
E.g.:
            IEnumerable<string> whereList = mList.Where(name =>
                {
                    if (name.Length > 3)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                });
         foreach (string s in subList)
         {
             Console.WriteLine("element in subList: "+s);
         }
         這時subList存儲的就是所有長度大于3的元素

(7)List.RemoveAll方法:移除與指定的謂詞所定義的條件相匹配的所有元素。
public int RemoveAll(Predicate<T> match);
E.g.:
            mList.RemoveAll(name =>
                {
                    if (name.Length > 3)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                });
            foreach (string s in mList)
            {
                Console.WriteLine("element in mList:     " + s);
            }
      這時mList存儲的就是移除長度大于3之后的元素。


該文章在 2021/3/10 11:26:49 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved