IList:首先IList 泛型接口是 ICollection 泛型接口的子代,並且是所有泛型列表的基接口。
它僅僅是所有泛型類型的接口,並沒有太多方法可以方便實用,如果僅僅是作為集合數據的承載體,確實,IList<T>可以勝任。
不過,更多的時候,我們要對集合數據進行處理,從中篩選數據或者排序。這個時候IList<T>就愛莫能助了。
1、當你只想使用接口的方法時,ILis<>這種方式比較好.他不獲取實現這個接口的類的其他方法和字段,有效的節省空間.
2、IList <>是個接口,定義了一些操作方法這些方法要你自己去實現
List <>是泛型類,它已經實現了IList <>定義的那些方法
IList <Class1> IList11 =new List <Class1>();
List <Class1> List11 =new List <Class1>();
這兩行代碼,從操作上來看,實際上都是創建了一個List<Class1>對象的實例,也就是說,他們的操作沒有區別。
只是用於保存這個操作的返回值變量類型不一樣而已。
那么,我們可以這么理解,這兩行代碼的目的不一樣。
List <Class1> List11 =new List <Class1>();
是想創建一個List<Class1>,而且需要使用到List<T>的功能,進行相關操作。
而
IList <Class1> IList11 =new List <Class1>();
只是想創建一個基於接口IList<Class1>的對象的實例,只是這個接口是由List<T>實現的。所以它只是希望使用到IList<T>接口規定的功能而已。
using System; using System.Collections.Generic; namespace ListStudy { /// <summary> /// 以下沒有接收返回值的方法都是屬於 void方法 /// </summary> class Program { static void Main(string[] args) { List<string> lists = new List<string>(); /* 添加元素 */ //添加一個元素 lists.Add("first"); //添加多個元素 string[] temArr = { "two", "three" }; lists.AddRange(temArr); //在index位置添加一個元素 lists.Insert(0, "zero"); //遍歷List中元素 foreach (string item in lists) { Console.Write(item + "/"); } //結果為:zero/first/two/three/ /* 刪除元素 */ //刪除一個值 bool remove = lists.Remove("first1"); //刪除下表為index的元素 lists.RemoveAt(3); //超出下標會報異常 //從下標index開始,刪除count個元素 lists.RemoveRange(2, 1); //超出下標或者個數超出list長度都會報異常 /* 判斷某個元素是否在list中 */ bool contains = lists.Contains("firsT");//屬於精確查找匹配,不是模糊匹配 Console.WriteLine(contains); /* 元素排序 */ lists.Sort(); //默認是元素第一個字母按升序 //給List里面元素順序反轉 lists.Reverse(); //注意這個需要與sort一起使用 /* list清空 */ lists.Clear(); /* list獲取元素個數 */ int count = lists.Count; /* list lambda表達式 */ string[] temArr1 = { "zero", "first", "two", "three" }; lists.AddRange(temArr1); string s = lists.Find(name => { if (name.Contains("w")) { return true; } else { return false; } }); //返回第一個模糊匹配的元素 //以下方法都可以使用這種方式 //List<string> ss = lists.FindAll(); //List<string> ss = lists.FindLast(); //返回最后一個模糊匹配的元素 //bool trueForAll = lists.TrueForAll(); //確定是否 List 中的每個元素都與指定的謂詞所定義的條件相匹配。比如是否每個元素的長度都超過了3 //IEnumerable<string> takeList= mList.Take(5); //獲得前n行 返回值為IEnumetable<T>,T的類型與List<T>的類型一樣 //IEnumerable<string> ss = lists.Where(); //檢索與指定謂詞所定義的條件相匹配的所有元素。跟List.FindAll方法類似。 //int num = lists.RemoveAll(); //移除與指定的謂詞所定義的條件相匹配的所有元素。並返回移除的數量 Console.Write($"s:{s}"); } } }
using System; namespace ListStudy { class Program { static void Main(string[] args) { //創建數組的幾種方法 //第一種 string[] myArray = new string[10]; //第二種 string[] myArray2 = { "first", "two" }; //第三種 string[] myArray3 = new string[3] { "first", "two", "there" }; //第四種 string[] myArray4 = new string[] { "first", "two", "there", "four" }; //數組排序 Array.Sort(myArray4); } } }
using System; namespace ListStudy { class Program { static void Main(string[] args) { int[] list = { 34, 72, 13, 44, 25, 30, 10 }; Console.Write("原始數組: "); foreach (int i in list) { Console.Write(i + " "); } Console.WriteLine(); // 逆轉數組 Array.Reverse(list); Console.Write("逆轉數組: "); foreach (int i in list) { Console.Write(i + " "); } Console.WriteLine(); // 排序數組 Array.Sort(list); Console.Write("排序數組: "); foreach (int i in list) { Console.Write(i + " "); } Console.WriteLine(); int[] list1 = new int[3]; Array.Copy(list, list1, 3);// 源數組,目標數組, 復制長度 Console.Write("Copy數組: "); foreach (int i in list1) { Console.Write(i + " "); } Console.WriteLine(); int[] list2 = new int[3]; Array.Copy(list, 3, list2, 0, 3);// 源數組,從源數組第幾個下標index開始復制,目標數組, 從目標數組的第幾個下表index開始放值, 復制長度 Console.Write("Copy數組: "); foreach (int i in list2) { Console.Write(i + " "); } Console.WriteLine(); Console.ReadKey(); } } }
using System.Collections.Generic; namespace ListStudy { class Program { static void Main(string[] args) { //數組轉List string[] temArr1 = { "zero", "first", "two", "three" }; List<string> lists = new List<string>(temArr1); //List轉數組 List<int> listss = new List<int>() { 1, 2, 3, 4, 5 }; int[] temArray2 = listss.ToArray(); } } }