在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
IList
IList
是 .NET Framework 的 Collections 命名空间的一部分。 它是一个非通用的集合接口,为可以通过索引单独访问的对象集合提供了蓝图。 与数组不同,IList
允许动态数量的对象值元素,这意味着您可以根据需要添加或删除集合中的元素。 它是 .NET Framework 中所有非通用列表的基础接口,提供了一种比数组更灵活的方式来管理对象集合。 在本教程中,我们将了解IList
接口和IronPDF C# PDF库。
IList
接口public interface IList
声明是在 C# 中创建自定义集合的基本组成部分,这些集合符合 .NET Framework 的 Collections 命名空间指定的 IList
合约。 IList
包含的属性和方法能够访问集合中的元素、对其进行计数,以及通过添加、插入或删除元素来修改集合。 以下是IList
接口中定义的一些关键属性和方法:
IsFixedSize
和IsReadOnly
这样的属性分别指示集合是否是固定大小或只读。Add
、void Insert
、Remove
和RemoveAt
用于修改集合中的元素。 您可以添加、插入和删除元素。IndexOf
方法用于定位元素,Item
属性(或在 C# 中的索引器)允许根据索引获取和设置元素。IList
的实际用法为了展示IList
的工作原理,我们来创建一个简单的例子。 这个通用版本示例将演示如何声明IList
,向其中添加项目,并迭代其内容。
IList
首先,让我们看看如何声明一个IList
并向其中添加项目:
using System;
using System.Collections;
class Program
{
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 interface using count property
Console.WriteLine($"Number of elements: {myIList.Count}");
// Access Elements
foreach (var element in myIList)
{
Console.WriteLine(element);
}
}
}
using System;
using System.Collections;
class Program
{
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 interface using count property
Console.WriteLine($"Number of elements: {myIList.Count}");
// Access Elements
foreach (var element in myIList)
{
Console.WriteLine(element);
}
}
}
Imports System
Imports System.Collections
Friend Class Program
Private 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 interface using count property
Console.WriteLine($"Number of elements: {myIList.Count}")
' Access Elements
For Each element In myIList
Console.WriteLine(element)
Next element
End Sub
End Class
在上述示例中,我们使用实现了IList
的类ArrayList
创建了一个IList
实例。 我们添加了不同类型对象的组合,以演示IList
可以容纳任何对象。 最后,我们迭代了这个集合,打印出了每个元素。
通过其索引访问和修改元素是IList
的一个关键功能。 以下IList
示例展示了如何实现:
// 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]}");
// 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]}");
' 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)}")
IList
有时候,您可能需要一个继承IList
的定制集合。 这可以对元素的存储、访问和修改方式进行更多控制。 下面是一个实现IList
的简单自定义集合的示例:
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;
// int add
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();
}
}
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;
// int add
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();
}
}
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
' int add
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
这个CustomCollection
类封装了一个ArrayList
,该类本身实现了IList
。 我们的CustomCollection
将调用转发到底层ArrayList
,使其表现得像任何其他实现IList
的集合。 这个例子演示了如何创建一个可以通过索引访问、修改(添加、插入或删除项目)并进行迭代的集合,就像任何实现了IList
接口的内置.NET集合一样。
IList
的高级操作除了基本的添加、移除和访问操作之外,IList
还允许进行更复杂的操作和查询。 例如,检查集合中是否包含特定对象或查找集合中对象的索引,这些操作对于某些应用程序来说至关重要:
// 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}");
// 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}");
' 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}")
在处理对象集合时,如果需要确定特定项目的存在或位置,而不需要遍历整个集合,这些操作会特别有用。
IronPDF 是面向 .NET 开发人员的 PDF 库,可直接在 .NET 应用程序中创建和处理 PDF 文档。 它支持将HTML 转换为 PDF 文档、图像和网页转换为 PDF。 开发人员可以使用该库轻松地为其应用程序添加 PDF 功能。 IronPdf 还包括编辑、合并和拆分 PDF 文件的功能,可对 PDF 操作进行全面控制。
IronPDF 在HTML 转 PDF转换方面表现出色,确保原始布局和样式的精确保留。 它非常适合从基于网络的内容(如报告、发票和文档)创建PDF。 IronPDF 支持 HTML 文件、URL 和原始 HTML 字符串,能够轻松生成高质量的 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
这是一个简单的示例,演示如何使用 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
在此示例中,使用IList<string>
来存储一组水果名称。 GeneratePDFFromList
方法随后迭代这个列表,构建一个包含每个项的无序列表的 HTML 字符串。IronPDF 的 ChromePdfRenderer
将此 HTML 内容转换为 PDF 文档,随后将其保存到文件中。
本初学者友好指南旨在介绍C#中IList
的基础知识和实际用途。 从简单使用到自定义实现的示例表明,IList
是C#开发者工具包中的一个强大工具。 无论您是在操作数据集合还是构建自己的集合类型,IList
都提供了有效软件开发所需的功能和灵活性。 IronPDF 为感兴趣的用户提供PDF 库的免费试用,许可证起价为$749。