最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中IList 與 List 的區(qū)別小結(jié)

 更新時間:2024年04月15日 09:17:38   作者:那個那個魚  
IList 接口和 List 類是 C# 中用于集合操作的兩個重要的類型,本文主要介紹了C#中IList 與 List 的區(qū)別小結(jié),具有一定的參考價值,感興趣的可以了解一下

IList 接口和 List 類是 C# 中用于集合操作的兩個重要的類型。

它們之間的區(qū)別如下:

1. 定義和實現(xiàn)方式:

IList 接口是一個抽象接口,定義了一組用于操作列表的方法和屬性。它是 System.Collections 命名空間中的一部分,可以被其他類實現(xiàn)。
List 類是 IList 接口的一個具體實現(xiàn),它提供了 IList 接口中定義的所有方法和屬性的具體實現(xiàn)。List 類位于 System.Collections.Generic 命名空間中。

2. 泛型支持:

IList 接口是非泛型接口,它可以存儲任意類型的對象。
List 類是泛型類,它可以指定存儲的元素類型,并在編譯時進行類型檢查,提供更好的類型安全性。

3. 功能和性能:

IList 接口定義了一組基本的列表操作方法,如添加、刪除、插入、索引訪問等。它提供了對列表的基本操作支持,但不提供具體的實現(xiàn)。
List 類在 IList 接口的基礎(chǔ)上提供了更多的功能和性能優(yōu)化。它使用動態(tài)數(shù)組來存儲元素,可以高效地進行插入、刪除和索引訪問操作。此外,List 類還提供了一些額外的方法,如排序、查找等。

錯誤使用案例

using System;
using System.Collections.Generic;
namespace DemoApplication{
   class Demo{
      static void Main(string[] args){
         IList<string> ilist = new IList<string>();
         //This will throw error as we cannot create instance for an IList as it is an interface.
         ilist.Add("Mark");
         ilist.Add("John");
         foreach (string list in ilist){
            Console.WriteLine(list);
         }
      }
   }
}

下面的是正確案例

using System;
using System.Collections.Generic;
namespace DemoApplication{
   class Demo{
      static void Main(string[] args){
         IList<string> ilist = new List<string>();
         ilist.Add("Mark");
         ilist.Add("John");
         List<string> list = new List<string>();
         ilist.Add("Mark");
         ilist.Add("John");
         foreach (string lst in ilist){
            Console.WriteLine(lst);
         }
         foreach (string lst in list){
            Console.WriteLine(lst);
         }
         Console.ReadLine();
      }
   }
}

List轉(zhuǎn)IList的方法

/// <summary>
/// List轉(zhuǎn)IList公共方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="listObjects"></param>
/// <returns></returns>
protected static IList<T> ConvertToGenericList<T>(IList listObjects)
{
      IList<T> convertedList = new List<T>(listObjects.Count);
 
      foreach (object listObject in listObjects)
      {
           convertedList.Add((T)listObject);
      }
 
      return convertedList;
}

總結(jié):

  • IList 接口是一個抽象的列表操作接口,可以被其他類實現(xiàn)。
  • List 類是 IList 接口的一個具體實現(xiàn),提供了更多的功能和性能優(yōu)化。

到此這篇關(guān)于C#中IList 與 List 的區(qū)別小結(jié)的文章就介紹到這了,更多相關(guān)C# IList List 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評論

镶黄旗| 阳信县| 宜宾县| 龙岩市| 卢龙县| 张家界市| 上饶县| 襄城县| 霞浦县| 铅山县| 临沭县| 托克逊县| 阳西县| 横峰县| 朝阳区| 乌拉特后旗| 周宁县| 论坛| 沐川县| 江华| 炎陵县| 贡嘎县| 酒泉市| 富宁县| 东丰县| 天台县| 新乡县| 静乐县| 桦甸市| 瑞昌市| 舞阳县| 曲周县| 盐山县| 宿迁市| 雅安市| 喀什市| 馆陶县| 绥化市| 霞浦县| 虹口区| 孟津县|