.NET幫助 C# iList(對於開發者的運行原理) Curtis Chau 更新日期:7月 28, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 介紹 IList IList 是 .NET Framework 集合命名空間的一部分。 它是一個非泛型集合接口,提供對可以通過其索引單獨訪問的對象集合的藍圖。 與數組不同,IList 允許動態數量的對象值元素,這意味著您可以根據需要從集合中添加或刪除元素。 它作為 .NET Framework 中所有非泛型列表的基本接口,提供了一種比數組更靈活地管理對象集合的方式。 在本教程中,我們將學習 IList 接口和 IronPDF C# PDF 庫。 理解 IList 接口 public interface IList 聲明是創建遵循由 .NET Framework 集合命名空間指定的 IList 合約的自定義集合的基本部分。 IList 包括使得能夠訪問集合中元素、計數元素和通過添加、插入或刪除元素來修改集合的屬性和方法。 以下是一些 IList 接口中定義的關鍵屬性和方法: 像 IsFixedSize 和 IsReadOnly 這樣的屬性分別告知集合是固定大小還是只讀。 像 Add、Insert、Remove 和 RemoveAt 這樣的方法用於修改集合中的元素。 您可以添加、插入和刪除元素。 IndexOf 方法用於定位元素,Item 屬性(或 C# 中的索引器)允許根據索引獲取和設置元素。 IList 接口的實際應用 為了展示 IList 的工作原理,讓我們創建一個簡單的示例。 此示例將演示如何聲明 IList、添加項目以及遍歷其內容。 創建和修改 IList 首先,讓我們看看如何聲明 IList 並向其添加項目: using System; using System.Collections; class Program { static void Main(string[] args) { // Creating an IList instance IList myIList = new ArrayList(); // Adding elements to the IList myIList.Add("Hello"); myIList.Add(10); myIList.Add(new object()); // Displaying the number of values in the IList using the Count property Console.WriteLine($"Number of elements: {myIList.Count}"); // Accessing elements using a loop foreach (var element in myIList) { Console.WriteLine(element); } } } using System; using System.Collections; class Program { static void Main(string[] args) { // Creating an IList instance IList myIList = new ArrayList(); // Adding elements to the IList myIList.Add("Hello"); myIList.Add(10); myIList.Add(new object()); // Displaying the number of values in the IList using the Count property Console.WriteLine($"Number of elements: {myIList.Count}"); // Accessing elements using a loop foreach (var element in myIList) { Console.WriteLine(element); } } } Imports System Imports System.Collections Friend Class Program Shared Sub Main(ByVal args() As String) ' Creating an IList instance Dim myIList As IList = New ArrayList() ' Adding elements to the IList myIList.Add("Hello") myIList.Add(10) myIList.Add(New Object()) ' Displaying the number of values in the IList using the Count property Console.WriteLine($"Number of elements: {myIList.Count}") ' Accessing elements using a loop For Each element In myIList Console.WriteLine(element) Next element End Sub End Class $vbLabelText $csharpLabel 在上述示例中,我們使用實現 IList 的 ArrayList 創建了一個 IList 實例。 我們添加了不同類型對象的混合,以演示 IList 可以容納任何對象。 最後,我們遍歷了這個集合,打印出每個元素。 基於索引的訪問和修改 根據索引訪問和修改元素是 IList 的一個關鍵特徵。 以下示例顯示了您如何做到這一點: using System; using System.Collections; class Program { static void Main(string[] args) { IList myIList = new ArrayList { "Hello", 10, new object() }; // Accessing an element by index object value = myIList[1]; Console.WriteLine($"Element at index 1: {value}"); // Modifying an element by index myIList[1] = 20; Console.WriteLine($"Modified element at index 1: {myIList[1]}"); } } using System; using System.Collections; class Program { static void Main(string[] args) { IList myIList = new ArrayList { "Hello", 10, new object() }; // Accessing an element by index object value = myIList[1]; Console.WriteLine($"Element at index 1: {value}"); // Modifying an element by index myIList[1] = 20; Console.WriteLine($"Modified element at index 1: {myIList[1]}"); } } Imports System Imports System.Collections Friend Class Program Shared Sub Main(ByVal args() As String) Dim myIList As IList = New ArrayList From { "Hello", 10, New Object() } ' Accessing an element by index Dim value As Object = myIList(1) Console.WriteLine($"Element at index 1: {value}") ' Modifying an element by index myIList(1) = 20 Console.WriteLine($"Modified element at index 1: {myIList(1)}") End Sub End Class $vbLabelText $csharpLabel 實施自定義 IList 有時,您可能需要一個繼承自 IList 的定制集合。 這允許對存儲、訪問和修改元素的方式進行更多控制。 以下是一個實施 IList 的簡單自定義集合的示例: using System; using System.Collections; public class CustomCollection : IList { private ArrayList _innerList = new ArrayList(); public object this[int index] { get => _innerList[index]; set => _innerList[index] = value; } public bool IsFixedSize => _innerList.IsFixedSize; public bool IsReadOnly => _innerList.IsReadOnly; public int Count => _innerList.Count; public bool IsSynchronized => _innerList.IsSynchronized; public object SyncRoot => _innerList.SyncRoot; public int Add(object value) { return _innerList.Add(value); } public void Clear() { _innerList.Clear(); } public bool Contains(object value) { return _innerList.Contains(value); } public int IndexOf(object value) { return _innerList.IndexOf(value); } public void Insert(int index, object value) { _innerList.Insert(index, value); } public void Remove(object value) { _innerList.Remove(value); } public void RemoveAt(int index) { _innerList.RemoveAt(index); } public void CopyTo(Array array, int index) { _innerList.CopyTo(array, index); } public IEnumerator GetEnumerator() { return _innerList.GetEnumerator(); } } using System; using System.Collections; public class CustomCollection : IList { private ArrayList _innerList = new ArrayList(); public object this[int index] { get => _innerList[index]; set => _innerList[index] = value; } public bool IsFixedSize => _innerList.IsFixedSize; public bool IsReadOnly => _innerList.IsReadOnly; public int Count => _innerList.Count; public bool IsSynchronized => _innerList.IsSynchronized; public object SyncRoot => _innerList.SyncRoot; public int Add(object value) { return _innerList.Add(value); } public void Clear() { _innerList.Clear(); } public bool Contains(object value) { return _innerList.Contains(value); } public int IndexOf(object value) { return _innerList.IndexOf(value); } public void Insert(int index, object value) { _innerList.Insert(index, value); } public void Remove(object value) { _innerList.Remove(value); } public void RemoveAt(int index) { _innerList.RemoveAt(index); } public void CopyTo(Array array, int index) { _innerList.CopyTo(array, index); } public IEnumerator GetEnumerator() { return _innerList.GetEnumerator(); } } Imports System Imports System.Collections Public Class CustomCollection Implements IList Private _innerList As New ArrayList() Default Public Property Item(ByVal index As Integer) As Object Implements IList.Item Get Return _innerList(index) End Get Set(ByVal value As Object) _innerList(index) = value End Set End Property Public ReadOnly Property IsFixedSize() As Boolean Implements IList.IsFixedSize Get Return _innerList.IsFixedSize End Get End Property Public ReadOnly Property IsReadOnly() As Boolean Implements IList.IsReadOnly Get Return _innerList.IsReadOnly End Get End Property Public ReadOnly Property Count() As Integer Implements System.Collections.ICollection.Count Get Return _innerList.Count End Get End Property Public ReadOnly Property IsSynchronized() As Boolean Implements System.Collections.ICollection.IsSynchronized Get Return _innerList.IsSynchronized End Get End Property Public ReadOnly Property SyncRoot() As Object Implements System.Collections.ICollection.SyncRoot Get Return _innerList.SyncRoot End Get End Property Public Function Add(ByVal value As Object) As Integer Implements IList.Add Return _innerList.Add(value) End Function Public Sub Clear() Implements IList.Clear _innerList.Clear() End Sub Public Function Contains(ByVal value As Object) As Boolean Implements IList.Contains Return _innerList.Contains(value) End Function Public Function IndexOf(ByVal value As Object) As Integer Implements IList.IndexOf Return _innerList.IndexOf(value) End Function Public Sub Insert(ByVal index As Integer, ByVal value As Object) Implements IList.Insert _innerList.Insert(index, value) End Sub Public Sub Remove(ByVal value As Object) Implements IList.Remove _innerList.Remove(value) End Sub Public Sub RemoveAt(ByVal index As Integer) Implements IList.RemoveAt _innerList.RemoveAt(index) End Sub Public Sub CopyTo(ByVal array As Array, ByVal index As Integer) Implements System.Collections.ICollection.CopyTo _innerList.CopyTo(array, index) End Sub Public Function GetEnumerator() As IEnumerator Implements System.Collections.IEnumerable.GetEnumerator Return _innerList.GetEnumerator() End Function End Class $vbLabelText $csharpLabel 此 CustomCollection 類封裝了一個 ArrayList,這是一個自身實現了 IList 的類。 我們的 CustomCollection 轉發對底層 ArrayList 的調用,使其能像任何其他實現 IList 的集合一樣運作。 此示例展示了創建一個可以通過索引訪問、可修改(添加、插入或刪除項目)並可遍歷的集合,就像任何實現 IList 的內置 .NET 集合一樣。 使用 IList 進行高級操作 除了基本的添加、刪除和訪問操作,IList 還允許更複雜的操作和查詢。 例如,檢查集合中是否包含特定對象或在集合中查找某個對象的索引,是某些應用程序中至關重要的操作: using System; using System.Collections; class Program { static void Main(string[] args) { IList myIList = new ArrayList { "Hello", 10, new object() }; // Check if the IList contains a specific object bool contains = myIList.Contains(10); // Assuming 10 was added previously Console.WriteLine($"Contains 10: {contains}"); // Find the index of a specific object int index = myIList.IndexOf(10); Console.WriteLine($"Index of 10: {index}"); } } using System; using System.Collections; class Program { static void Main(string[] args) { IList myIList = new ArrayList { "Hello", 10, new object() }; // Check if the IList contains a specific object bool contains = myIList.Contains(10); // Assuming 10 was added previously Console.WriteLine($"Contains 10: {contains}"); // Find the index of a specific object int index = myIList.IndexOf(10); Console.WriteLine($"Index of 10: {index}"); } } Imports System Imports System.Collections Friend Class Program Shared Sub Main(ByVal args() As String) Dim myIList As IList = New ArrayList From { "Hello", 10, New Object() } ' Check if the IList contains a specific object Dim contains As Boolean = myIList.Contains(10) ' Assuming 10 was added previously Console.WriteLine($"Contains 10: {contains}") ' Find the index of a specific object Dim index As Integer = myIList.IndexOf(10) Console.WriteLine($"Index of 10: {index}") End Sub End Class $vbLabelText $csharpLabel 在處理對象集合時,這些操作特別有用,您需要在不遍歷整個集合的情況下確定特定項目是否存在或位置。 IronPDF:C# PDF 庫 IronPDF 是一個面向 .NET 開發者的 PDF 庫,允許直接在 .NET 應用程序中創建和操作 PDF 文檔。 它支持將HTML 轉換為 PDF 文檔,圖像和網頁轉換為 PDF。 開發者可以輕鬆地使用此庫為其應用程序添加 PDF 功能。 IronPDF 還包括編輯、合併和拆分 PDF 文件的功能,提供了對 PDF 操作的全面控制。 IronPDF 在HTML 到 PDF轉換方麵表現出色,確保準確保持原始佈局和樣式。 它非常適合從網路內容生成 PDF,如報告、發票和文檔。 支持 HTML 文件、URL 和原始 HTML 字串的 IronPDF 可以輕鬆生成高質量的 PDF 文檔。 using IronPdf; class Program { static void Main(string[] args) { var renderer = new ChromePdfRenderer(); // 1. Convert HTML String to PDF var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"; var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent); pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf"); // 2. Convert HTML File to PDF var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath); pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf"); // 3. Convert URL to PDF var url = "http://ironpdf.com"; // Specify the URL var pdfFromUrl = renderer.RenderUrlAsPdf(url); pdfFromUrl.SaveAs("URLToPDF.pdf"); } } using IronPdf; class Program { static void Main(string[] args) { var renderer = new ChromePdfRenderer(); // 1. Convert HTML String to PDF var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"; var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent); pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf"); // 2. Convert HTML File to PDF var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath); pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf"); // 3. Convert URL to PDF var url = "http://ironpdf.com"; // Specify the URL var pdfFromUrl = renderer.RenderUrlAsPdf(url); pdfFromUrl.SaveAs("URLToPDF.pdf"); } } Imports IronPdf Friend Class Program Shared Sub Main(ByVal args() As String) Dim renderer = New ChromePdfRenderer() ' 1. Convert HTML String to PDF Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>" Dim pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent) pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf") ' 2. Convert HTML File to PDF Dim htmlFilePath = "path_to_your_html_file.html" ' Specify the path to your HTML file Dim pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath) pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf") ' 3. Convert URL to PDF Dim url = "http://ironpdf.com" ' Specify the URL Dim pdfFromUrl = renderer.RenderUrlAsPdf(url) pdfFromUrl.SaveAs("URLToPDF.pdf") End Sub End Class $vbLabelText $csharpLabel 代碼示例 這裡有一個簡單的例子,演示如何使用 IronPDF 和 IList 接口從字符串列表生成簡單的 PDF 文檔: using IronPdf; using System.Collections.Generic; public class PDFGenerator { public static void GeneratePDFFromList(IList<string> dataList) { // Initialize the HtmlToPdf renderer var renderer = new ChromePdfRenderer(); // Start building HTML content from the dataList var htmlContent = "<h1>My Data List</h1><ul>"; foreach (var item in dataList) { htmlContent += $"<li>{item}</li>"; } htmlContent += "</ul>"; // Convert HTML string to PDF var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent); // Save the PDF to a file pdfDocument.SaveAs("DataList.pdf"); } } // Example usage class Program { static void Main(string[] args) { License.LicenseKey = "License-Key"; IList<string> myDataList = new List<string> { "Apple", "Banana", "Cherry" }; PDFGenerator.GeneratePDFFromList(myDataList); } } using IronPdf; using System.Collections.Generic; public class PDFGenerator { public static void GeneratePDFFromList(IList<string> dataList) { // Initialize the HtmlToPdf renderer var renderer = new ChromePdfRenderer(); // Start building HTML content from the dataList var htmlContent = "<h1>My Data List</h1><ul>"; foreach (var item in dataList) { htmlContent += $"<li>{item}</li>"; } htmlContent += "</ul>"; // Convert HTML string to PDF var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent); // Save the PDF to a file pdfDocument.SaveAs("DataList.pdf"); } } // Example usage class Program { static void Main(string[] args) { License.LicenseKey = "License-Key"; IList<string> myDataList = new List<string> { "Apple", "Banana", "Cherry" }; PDFGenerator.GeneratePDFFromList(myDataList); } } Imports IronPdf Imports System.Collections.Generic Public Class PDFGenerator Public Shared Sub GeneratePDFFromList(ByVal dataList As IList(Of String)) ' Initialize the HtmlToPdf renderer Dim renderer = New ChromePdfRenderer() ' Start building HTML content from the dataList Dim htmlContent = "<h1>My Data List</h1><ul>" For Each item In dataList htmlContent &= $"<li>{item}</li>" Next item htmlContent &= "</ul>" ' Convert HTML string to PDF Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent) ' Save the PDF to a file pdfDocument.SaveAs("DataList.pdf") End Sub End Class ' Example usage Friend Class Program Shared Sub Main(ByVal args() As String) License.LicenseKey = "License-Key" Dim myDataList As IList(Of String) = New List(Of String) From {"Apple", "Banana", "Cherry"} PDFGenerator.GeneratePDFFromList(myDataList) End Sub End Class $vbLabelText $csharpLabel 在這個例子中,使用了 IList<string> 來存儲一個水果名稱的集合。 GeneratePDFFromList 方法接著遍歷這個列表,構建了一個包含每個項目的無序列表的 HTML 字符串。IronPDF 的 ChromePdfRenderer 將這個 HTML 內容轉換為 PDF 文檔,然後保存為一個文件。 結論 這本初學者指南旨在涵蓋 C# 中 IList 的基礎知識和實際應用。 從簡單應用到自定義實現範例不一,IList 無疑是 C# 開發者工具箱中強大的工具。 無論您是操縱數據集合還是構建自己的集合類型,IList 提供了高效軟件開發所需的功能和靈活性。 IronPDF 為感興趣的用戶提供其 PDF 庫的免費試用,並提供起始價格從 $799 的許可。 常見問題解答 什麼是C#中的IList介面? `IList`介面是.NET Framework的Collections命名空間的一部分,用於創建可以通過索引訪問的動態集合。它提供了添加、插入、刪除和訪問元素的方法,提供比靜態數組更大的靈活性。 如何在 C# 中將 HTML 轉換為 PDF? 可以使用IronPDF的`RenderHtmlAsPdf`方法在C#中將HTML轉換為PDF。這允許您將HTML字串、文件或URL轉換為高質量的PDF文件。 IList介面的一些關鍵方法是什麼? `IList`介面的關鍵方法包括`Add`、`Insert`、`Remove`、`RemoveAt`和`IndexOf`。這些方法對動態管理和修改集合內的元素至關重要。 如何在C#中創建自定義的IList? 要在C#中創建自定義的`IList`,您可以在一個類中實現`IList`介面,重寫必要的方法和屬性,如`Add`、`Remove`和`IndexOf`,以自定義集合如何處理其元素。 IronPDF如何從HTML創建PDF? IronPDF使用`ChromePdfRenderer`類將HTML內容轉換為PDF。它支持從HTML字串、文件或URL進行轉換,確保從網絡內容準確地創建PDF。 可以從IList中的數據生成PDF嗎? 是的,您可以從IList中生成PDF,通過遍歷列表構建HTML字串,然後使用IronPDF的`ChromePdfRenderer`將HTML轉換為PDF文件,並可以使用`SaveAs`方法保存。 IList支持哪些高級操作? `IList`中的高級操作包括使用`Contains`檢查集合中是否包含特定對象,以及使用`IndexOf`查找對象的位置。這些功能有助於有效管理集合,而無需手動搜索元素。 如何在C#中排除PDF生成問題? 如果您在C#中遇到PDF生成問題,請確保您的HTML內容格式正確,並且您正在使用IronPDF的最新版本。檢查在轉換過程中引發的任何異常以獲取進一步的洞察。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新日期 9月 4, 2025 RandomNumberGenerator C# 使用RandomNumberGenerator C#類可以幫助將您的PDF生成和編輯項目提升至新水準 閱讀更多 更新日期 9月 4, 2025 C#字符串等於(它如何對開發者起作用) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 更新日期 8月 5, 2025 C#開關模式匹配(對開發者來說是如何工作的) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 C# Linked List(對於開發者的運行原理)Polly Retry(對於開發者的運...