.NET幫助 C# 打印列表:快速教程 Jacob Mellor 更新:2025年7月28日 下載 IronPrint NuGet 下載 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 在 C# 中,列印清單是一項常見的任務,可以透過多種方式實現,從而提供靈活性和自訂性。 清單是 C# 中的基本資料結構,能夠列印其內容對於偵錯、日誌記錄或向使用者顯示資訊至關重要。 在本文中,我們將探討在 C# 中列印清單的不同方法。 C# 中列表的基礎知識 在 C# 中,數組(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 }; } } $vbLabelText $csharpLabel 使用循環列印列表 1. 使用 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); } } } $vbLabelText $csharpLabel 這種方法簡潔易懂,適用於大多數情況。 2. 使用 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]}"); } } } $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); } } } $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); } } } $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}"); } } $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}"); } } $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."); } } } $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); } } $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)); } } $vbLabelText $csharpLabel 在這個例子中,LINQ 用於從原始列表中過濾掉偶數。 Where() 方法與 lambda 表達式一起使用,ToList() 方法用於將結果轉換回列表。 自訂物件和 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); } } } $vbLabelText $csharpLabel 透過重寫 ToString() 類別中的 Person 方法,您可以控制該類別的實例如何表示為字串。 這樣可以提高列印出來的清單的可讀性。 隆重介紹IronPrint - C# 列印庫 IronPrint是一款功能強大且易於部署的列印庫,它優先考慮準確性、易用性和速度。 它具有跨平台支援和與各種文件格式的兼容性,對於尋求在應用程式中實現高效列印解決方案的.NET開發人員來說,它是一款非常有價值的工具。 主要特點 以下是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"); } } $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"); } } $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); } } $vbLabelText $csharpLabel 若要更了解所使用的類別和方法,請造訪API 參考頁面。 結論 在 C# 中列印清單需要根據資料的複雜性和所需的輸出選擇正確的方法。 無論是使用簡單的循環、LINQ,還是為自訂物件自訂方法,了解這些方法都能幫助您有效地在 C# 應用程式中顯示清單的內容。 嘗試這些方法,選擇最適合您特定使用情況的方法。 如果您想要精準、易用且快速的列印庫, IronPrint是您的理想之選。 無論您是在建立 Web 應用程式、使用 MAUI、Avalonia 或任何與 .NET 相關的項目, IronPrint都能為您提供支援。 有關IronPrint的更多詳細信息,請訪問此文件頁面。 IronPrint是一個付費圖書館,但提供免費試用。 從這裡下載庫文件並試用一下! 常見問題解答 如何在C#中打印列表? 您可以使用IronPrint在C#中打印列表,方法是通過foreach或for循環迭代列表元素,並將格式化的輸出傳遞給IronPrint打印。 使用動態數組在C#中有哪些好處? C#中的動態數組(或列表)提供了靈活性,因為它們可以增長或縮小大小。它們是System.Collections.Generic命名空間的一部分,允許有效地處理和操作集合。 如何在C#中反轉列表以進行打印? 要以反向順序打印列表,您可以使用Reverse方法或LINQ的OrderByDescending然後使用IronPrint打印反轉的列表。 如何將列表格式化為字串以進行打印? 您可以使用String.Join方法將列表元素連接成具有指定分隔符的單個格式化字串。然後可以使用IronPrint打印此格式化字串。 ToString方法在打印自定義物件中的作用是什麼? 通過覆蓋ToString方法,您可以定義自定義物件實例如何表示為字串,這在使用IronPrint打印時提高了可讀性。 如何在C#中篩選並打印列表中特定的元素? 可以使用LINQ方法如Where來篩選並選擇列表中的特定元素。然後可以使用IronPrint打印篩選的結果。 如何安裝.NET的打印庫? 您可以通過NuGet包管理控制台使用命令Install-Package IronPrint或通過Visual Studio的NuGet包管理器安裝IronPrint。 IronPrint為.NET開發人員提供了哪些功能? IronPrint提供跨平台兼容性,支持多種文件格式,靜默打印,打印對話框和可自定義的打印設置,使其成為.NET開發人員的強大選擇。 Jacob Mellor 立即與工程團隊聊天 首席技術官 Jacob Mellor是Iron Software的首席技術官,也是開創C# PDF技術的前瞻性工程師。作為Iron Software核心代碼庫的原始開發者,他自公司成立以來就塑造了公司的產品架構,並與CEO Cameron Rimington將公司轉型為服務NASA、Tesla以及全球政府機構的50多人公司。Jacob擁有曼徹斯特大學土木工程一級榮譽學士學位(1998年–2001年)。他於1999年在倫敦開立首家軟體公司,並於2005年建立了他的第一個.NET組件,專注於解決Microsoft生態系統中的複雜問題。他的旗艦作品IronPDF和Iron Suite .NET程式庫全球已獲得超過3000萬次NuGet安裝,他的基礎代碼不斷在全球各地驅動開發者工具。擁有25年以上的商業經驗和41年的編碼專業知識,Jacob仍然專注於推動企業級C#、Java和Python PDF技術的創新,同時指導下一代技術領導者。 相關文章 更新2025年7月28日 如何有效地使用 C# 打印行 在本文中,我們將探索與 C# 打印行有關的各種方法和技術。 閱讀更多 更新2025年6月22日 C# 打印變量:簡化您的代碼 在這篇綜合文章中,我們將探索在 C# 中打印變量的各種方面,涵蓋不同的數據類型,格式化選項以及高級技術。 閱讀更多 更新2025年7月28日 精通 C# 打印函數:開發者指南 C# 打印的核心在於 Console.WriteLine 方法。這是顯示控制台上格式化輸出信息的首選函數 閱讀更多 精通 C# 打印函數:開發者指南C# 打印控制台:逐步指南