.NET 帮助 C# 打印列表:快速教程 Jacob Mellor 已更新:七月 28, 2025 下载 IronPrint NuGet 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 在 C# 中,打印列表是一项常见的任务,可以通过多种方式实现,从而提供灵活性和自定义性。 列表是 C# 中的基本数据结构,能够打印其内容对于调试、日志记录或向用户显示信息至关重要。 在本文中,我们将探讨在 C# 中打印列表的不同方法。 C# 中列表的基础知识 在 C# 中, list是一种动态数组,其大小可以增长或缩小。它属于System.Collections.Generic命名空间,为处理项目集合提供了灵活性和高效性。 以下代码创建一个简单的数字列表: using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; } } using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; } } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 使用循环打印列表 1. 使用foreach循环 打印列表元素的传统且直接的方法是使用foreach循环。以下是一个简单的示例: using System; using System.Collections.Generic; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; Console.WriteLine("Printing list using foreach loop:"); foreach (var number in numbers) { Console.WriteLine(number); } } } using System; using System.Collections.Generic; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; Console.WriteLine("Printing list using foreach loop:"); foreach (var number in numbers) { Console.WriteLine(number); } } } Imports System Imports System.Collections.Generic Friend Class Program Public Shared Sub Main() Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5} Console.WriteLine("Printing list using foreach loop:") For Each number In numbers Console.WriteLine(number) Next number End Sub End Class $vbLabelText $csharpLabel 这种方法简洁易懂,适用于大多数情况。 2. 使用for循环 如果您需要更精确地控制索引或希望有条件地打印元素,可以使用for循环。以下是一个字符串列表的示例: using System; using System.Collections.Generic; class Program { public static void Main() { // Create list of strongly typed objects List<string> colors = new List<string> { "Red", "Green", "Blue", "Yellow" }; Console.WriteLine("Printing list using for loop:"); for (int index = 0; index < colors.Count; index++) { Console.WriteLine($"Color at index {index}: {colors[index]}"); } } } using System; using System.Collections.Generic; class Program { public static void Main() { // Create list of strongly typed objects List<string> colors = new List<string> { "Red", "Green", "Blue", "Yellow" }; Console.WriteLine("Printing list using for loop:"); for (int index = 0; index < colors.Count; index++) { Console.WriteLine($"Color at index {index}: {colors[index]}"); } } } Imports System Imports System.Collections.Generic Friend Class Program Public Shared Sub Main() ' Create list of strongly typed objects Dim colors As New List(Of String) From {"Red", "Green", "Blue", "Yellow"} Console.WriteLine("Printing list using for loop:") For index As Integer = 0 To colors.Count - 1 Console.WriteLine($"Color at index {index}: {colors(index)}") Next index End Sub End Class $vbLabelText $csharpLabel 当您需要访问索引或希望在打印过程中应用特定逻辑时,这种方法是有益的。 反向打印列表元素 可以使用Reverse方法以相反的顺序打印列表。 此方法会反转列表中元素的顺序,从而允许您遍历并打印它们。 1. 使用 List.Reverse 方法 例如,您可以使用Reverse方法按相反的顺序打印列表,然后打印每个元素。 using System; using System.Collections.Generic; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; numbers.Reverse(); foreach (var number in numbers) { Console.WriteLine(number); } } } using System; using System.Collections.Generic; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; numbers.Reverse(); foreach (var number in numbers) { Console.WriteLine(number); } } } Imports System Imports System.Collections.Generic Friend Class Program Public Shared Sub Main() Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5} numbers.Reverse() For Each number In numbers Console.WriteLine(number) Next number End Sub End Class $vbLabelText $csharpLabel 2. 使用 LINQ OrderByDescending 您还可以使用带有键的OrderByDescending方法对 LINQ 通用类中的指定元素集合进行排序: using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; var reversedNumbers = numbers.OrderByDescending(n => n); foreach (var number in reversedNumbers) { Console.WriteLine(number); } } } using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; var reversedNumbers = numbers.OrderByDescending(n => n); foreach (var number in reversedNumbers) { Console.WriteLine(number); } } } Imports System Imports System.Collections.Generic Imports System.Linq Friend Class Program Public Shared Sub Main() Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5} Dim reversedNumbers = numbers.OrderByDescending(Function(n) n) For Each number In reversedNumbers Console.WriteLine(number) Next number End Sub End Class $vbLabelText $csharpLabel 元素数量计数和打印 计算列表中元素的数量是一个常见的操作。 Count属性可用于此目的。 一旦您计算出数量,您可以轻松打印出来。 1. 使用 List.Count 属性 using System; using System.Collections.Generic; class Program { public static void Main() { List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; int count = names.Count; Console.WriteLine($"Number of elements: {count}"); } } using System; using System.Collections.Generic; class Program { public static void Main() { List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; int count = names.Count; Console.WriteLine($"Number of elements: {count}"); } } Imports System Imports System.Collections.Generic Friend Class Program Public Shared Sub Main() Dim names As New List(Of String) From {"Alice", "Bob", "Charlie"} Dim count As Integer = names.Count Console.WriteLine($"Number of elements: {count}") End Sub End Class $vbLabelText $csharpLabel 2. 使用 LINQ 计数方法 如果更倾向于使用 LINQ,它也可以用于计数: using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main() { List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; int count = names.Count(); Console.WriteLine($"Number of elements: {count}"); } } using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main() { List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; int count = names.Count(); Console.WriteLine($"Number of elements: {count}"); } } Imports System Imports System.Collections.Generic Imports System.Linq Friend Class Program Public Shared Sub Main() Dim names As New List(Of String) From {"Alice", "Bob", "Charlie"} Dim count As Integer = names.Count() Console.WriteLine($"Number of elements: {count}") End Sub End Class $vbLabelText $csharpLabel 打印指定索引处的列表元素 打印指定索引处的元素需要访问该索引处的列表。 这样一来,您就可以访问不同位置的元素,并确保您能够处理潜在的索引超出范围的情况: using System; using System.Collections.Generic; class Program { public static void Main() { List<double> prices = new List<double> { 19.99, 29.99, 39.99 }; int indexToPrint = 1; if (indexToPrint >= 0 && indexToPrint < prices.Count) { Console.WriteLine($"Element at index {indexToPrint}: {prices[indexToPrint]}"); } else { Console.WriteLine("Index out of range."); } } } using System; using System.Collections.Generic; class Program { public static void Main() { List<double> prices = new List<double> { 19.99, 29.99, 39.99 }; int indexToPrint = 1; if (indexToPrint >= 0 && indexToPrint < prices.Count) { Console.WriteLine($"Element at index {indexToPrint}: {prices[indexToPrint]}"); } else { Console.WriteLine("Index out of range."); } } } Imports System Imports System.Collections.Generic Friend Class Program Public Shared Sub Main() Dim prices As New List(Of Double) From {19.99, 29.99, 39.99} Dim indexToPrint As Integer = 1 If indexToPrint >= 0 AndAlso indexToPrint < prices.Count Then Console.WriteLine($"Element at index {indexToPrint}: {prices(indexToPrint)}") Else Console.WriteLine("Index out of range.") End If End Sub End Class $vbLabelText $csharpLabel 这些示例为在各种场景下打印列表元素奠定了基础。 list类还提供了其他一些可用于打印的有用方法。 一些有用的方法包括: Remove: Remove()方法会删除 C# 列表中的第一个匹配项。 RemoveAll: RemoveAll()方法用于根据指定的谓词删除元素。 RemoveRange: RemoveRange()方法根据指定的索引和计数参数删除一系列元素。 Add: Add()方法只能在列表末尾添加一个元素。 AddRange: AddRange()方法可以将指定集合中的元素添加到末尾。 Clear: Clear()方法会从列表中删除所有元素。 Insert: Insert()方法将元素插入到列表中指定的索引位置。 ForEach: ForEach()方法用于对每个元素执行特定操作,例如,高效地打印每个列表值。 ToArray: ToArray()方法将列表复制到一个新数组中。 现在您可以选择最符合您需求的方法,提高 C# 代码的可读性和效率。 String.Join 方法 String.Join方法提供了一种简洁的方法,可以将列表中的元素用指定的分隔符连接成一个字符串。 这对于创建列表的格式化字符串表示很有用。 using System; using System.Collections.Generic; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; string result = string.Join(", ", numbers); Console.WriteLine(result); } } using System; using System.Collections.Generic; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; string result = string.Join(", ", numbers); Console.WriteLine(result); } } Imports System Imports System.Collections.Generic Friend Class Program Public Shared Sub Main() Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5} Dim result As String = String.Join(", ", numbers) Console.WriteLine(result) End Sub End Class $vbLabelText $csharpLabel 在这里,列表numbers的各个元素被连接成一个字符串,字符串之间用逗号和空格分隔,从而得到格式化的输出。 在打印列表元素之前也可以使用排序方法。 LINQ在高级场景中的应用 对于更复杂的场景,例如在打印之前需要筛选、转换或格式化元素,语言集成查询 ( LINQ ) 会很有帮助。 using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; List<int> evenNumbers = numbers.Where(n => n % 2 == 0).ToList(); Console.WriteLine("Even numbers: " + string.Join(", ", evenNumbers)); } } using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; List<int> evenNumbers = numbers.Where(n => n % 2 == 0).ToList(); Console.WriteLine("Even numbers: " + string.Join(", ", evenNumbers)); } } Imports System Imports System.Collections.Generic Imports System.Linq Friend Class Program Public Shared Sub Main() Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5} Dim evenNumbers As List(Of Integer) = numbers.Where(Function(n) n Mod 2 = 0).ToList() Console.WriteLine("Even numbers: " & String.Join(", ", evenNumbers)) End Sub End Class $vbLabelText $csharpLabel 在这个例子中,LINQ 用于从原始列表中筛选出偶数。Where Where()方法配合 lambda 表达式使用,而ToList()方法用于将结果转换回列表。 C# 打印列表(开发者操作指南):图 1 - 控制台输出:偶数:2、4 自定义对象和 ToString() 方法 如果您有一系列自定义对象,建议您在对象类中重写ToString方法,以便进行有意义的表示。 以下示例演示了如何操作: using System; using System.Collections.Generic; class Person { public string Name { get; set; } public int Age { get; set; } public override string ToString() { return $"{Name}, {Age} years old"; } } class Program { public static void Main() { List<Person> people = new List<Person> { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 25 } }; foreach (Person person in people) { Console.WriteLine(person); } } } using System; using System.Collections.Generic; class Person { public string Name { get; set; } public int Age { get; set; } public override string ToString() { return $"{Name}, {Age} years old"; } } class Program { public static void Main() { List<Person> people = new List<Person> { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 25 } }; foreach (Person person in people) { Console.WriteLine(person); } } } Imports System Imports System.Collections.Generic Friend Class Person Public Property Name() As String Public Property Age() As Integer Public Overrides Function ToString() As String Return $"{Name}, {Age} years old" End Function End Class Friend Class Program Public Shared Sub Main() Dim people As New List(Of Person) From { New Person With { .Name = "Alice", .Age = 30 }, New Person With { .Name = "Bob", .Age = 25 } } For Each person As Person In people Console.WriteLine(person) Next person End Sub End Class $vbLabelText $csharpLabel 通过重写Person类中的ToString()方法,您可以控制该类的实例如何表示为字符串。 这样可以提高打印出来的列表的可读性。 C# 打印列表(开发者使用方法):图 2 - 控制台输出 隆重推出 IronPrint - C# 打印库 IronPrint是一款功能强大且易于部署的打印库,它优先考虑准确性、易用性和速度。 它具有跨平台支持和与各种文档格式的兼容性,对于寻求在应用程序中实现高效打印解决方案的 .NET 开发人员来说,它是一款非常有价值的工具。 C# 打印列表(开发者使用方法):图 3 - IronPrint for .NET:C# 打印库 主要功能 以下是 IronPrint 在 C# 应用程序中打印纸质文档方面脱颖而出的一些重要关键特性: 1. 跨平台兼容性 .NET 版本支持:.NET 8、7、6、5 和 Core 3.1+ 操作系统:Windows(7+,Server 2016+),macOS(10+),iOS(11+),Android API 21+(v5"Lollipop") 项目类型:移动端(Xamarin、MAUI 和 Avalonia)、桌面端(WPF、MAUI 和 Windows Avalonia)、控制台(应用程序和库) 2. 支持的格式 处理各种文档格式,包括 PDF、PNG、HTML、TIFF、GIF、JPEG、IMAGE 和 BITMAP。 根据文档要求自定义打印设置。 3. 安装简便 使用 NuGet 包管理器控制台安装IronPrint库: Install-Package IronPrint 或者,您可以使用 Visual Studio 将其安装到您的项目中。 在解决方案资源管理器中右键单击项目,然后单击"管理解决方案的 NuGet 程序包管理器"。 在 NuGet 浏览选项卡中,搜索"ironprint",从搜索结果中选择最新版本的 IronPrint 包,然后单击"安装"按钮将其添加到您的项目中。 使用 IronPrint 进行打印:代码示例 1. 打印文档 IronPrint 提供使用简单Print方式的静默打印文档功能。 如果物理打印机不可用,则使用操作系统指定的默认打印机进行打印。 using IronPrint; class Program { static void Main() { // Print the document Printer.Print("newDoc.pdf"); } } using IronPrint; class Program { static void Main() { // Print the document Printer.Print("newDoc.pdf"); } } Imports IronPrint Friend Class Program Shared Sub Main() ' Print the document Printer.Print("newDoc.pdf") End Sub End Class $vbLabelText $csharpLabel 2. 带对话框的打印 它还提供了一种专门的方法来显示print对话框,以便在打印时更好地进行控制。 ShowPrintDialogAsync方法也可以用于异步打印。 using IronPrint; class Program { static void Main() { // Show print dialog Printer.ShowPrintDialog("newDoc.pdf"); } } using IronPrint; class Program { static void Main() { // Show print dialog Printer.ShowPrintDialog("newDoc.pdf"); } } Imports IronPrint Friend Class Program Shared Sub Main() ' Show print dialog Printer.ShowPrintDialog("newDoc.pdf") End Sub End Class $vbLabelText $csharpLabel 3. 自定义打印设置 IronPrint 提供各种print设置,可以对文档打印进行精细控制。 using IronPrint; class Program { static void Main() { // Configure print settings PrintSettings printSettings = new PrintSettings() { Dpi = 150, NumberOfCopies = 2, PaperOrientation = PaperOrientation.Portrait }; // Print the document Printer.Print("newDoc.pdf", printSettings); } } using IronPrint; class Program { static void Main() { // Configure print settings PrintSettings printSettings = new PrintSettings() { Dpi = 150, NumberOfCopies = 2, PaperOrientation = PaperOrientation.Portrait }; // Print the document Printer.Print("newDoc.pdf", printSettings); } } Imports IronPrint Friend Class Program Shared Sub Main() ' Configure print settings Dim printSettings As New PrintSettings() With { .Dpi = 150, .NumberOfCopies = 2, .PaperOrientation = PaperOrientation.Portrait } ' Print the document Printer.Print("newDoc.pdf", printSettings) End Sub End Class $vbLabelText $csharpLabel 要更好地了解所使用的类和方法,请访问API 参考页面。 结论 在 C# 中打印列表需要根据数据的复杂性和所需的输出选择正确的方法。 无论是使用简单的循环、 String.Join() 、LINQ,还是为自定义对象自定义ToString()方法,了解这些方法都能帮助您有效地在 C# 应用程序中显示列表的内容。 尝试这些方法,选择最适合您具体使用情况的方法。 如果您想要精准、易用和快速的打印库, IronPrint是您的理想之选。 无论您是在构建 Web 应用程序、使用 MAUI、Avalonia 还是任何与 .NET 相关的项目,IronPrint 都能为您提供支持。 有关 IronPrint 的更多详细信息,请访问此文档页面。 IronPrint 是一个付费图书馆,但提供免费试用。 从这里下载库文件并试用一下! 常见问题解答 我如何在 C# 中打印列表? 您可以通过使用 foreach 或 for 循环遍历列表元素并将格式化输出传递给 IronPrint 来打印 C# 中的列表。 使用 C# 中的动态数组有什么好处? C# 中的动态数组或列表提供了灵活性,因为它们可以增长或缩小。它们是 System.Collections.Generic 命名空间的一部分,可以有效地处理和操作集合。 我如何在 C# 中反转列表以进行打印? 要以相反顺序打印列表,您可以使用 Reverse 方法或 LINQ 的 OrderByDescending,然后使用 IronPrint 打印反转后的列表。 我如何将列表格式化为字符串以进行打印? 您可以使用 String.Join 方法将列表元素连接成具有指定分隔符的单个格式化字符串。 然后可以使用 IronPrint 打印此格式化字符串。 ToString 方法在打印自定义对象中的作用是什么? 通过重写 ToString 方法,您可以定义自定义对象实例如何表示为字符串,从而提高使用 IronPrint 打印时的可读性。 如何在 C# 中过滤和打印特定的列表元素? 可以使用 LINQ 方法,如 Where 来过滤和选择列表中的特定元素。 然后可以使用 IronPrint 打印过滤结果。 我如何安装 .NET 的打印库? 您可以使用命令 Install-Package IronPrint 或通过 Visual Studio 的 NuGet 包管理器通过 NuGet 包管理控制台安装 IronPrint。 IronPrint 为 .NET 开发人员提供了哪些功能? IronPrint 提供跨平台兼容性、支持各种文档格式、静默打印、打印对话框和可定制的打印设置,使其成为 .NET 开发人员的强大选择。 Jacob Mellor 立即与工程团队聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技术官,是 C# PDF 技术的先锋工程师。作为 Iron Software 核心代码库的原始开发者,自公司成立以来,他就塑造了公司的产品架构,并与首席执行官 Cameron Rimington 一起将其转变成一家公司,拥有50多人,服务于 NASA、特斯拉和全球政府机构。Jacob 拥有曼彻斯特大学 (1998-2001) 的一级荣誉土木工程学士学位。1999 年在伦敦创办了自己的第一家软件公司,并于 2005 年创建了他的第一个 .NET 组件后,他专注于解决微软生态系统中的复杂问题。他的旗舰 IronPDF 和 Iron Suite .NET 库在全球已获得超过 3000 万次的 NuGet 安装,其基础代码继续为全球使用的开发者工具提供支持。拥有 25 年商业经验和 41 年编程经验的 Jacob 仍专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。 相关文章 已更新七月 28, 2025 如何有效使用 C# 打印行 在本文中,我们将探索 C# 中打印行的各种方法和技术。 阅读更多 已更新六月 22, 2025 C# 打印变量:简化你的代码 在这篇详细的文章中,我们将探索 C# 中打印变量的各个方面,涵盖不同的数据类型、格式选项和高级技术。 阅读更多 已更新七月 28, 2025 掌握 C# 打印功能:开发者指南 C# 打印的核心在于 Console.WriteLine 方法。它是用于在控制台上显示格式化输出信息的重要功能。 阅读更多 掌握 C# 打印功能:开发者指南C# 控制台打印:分步指南