.NET幫助 C# 打印語句:學習基礎 Jacob Mellor 更新:2025年7月28日 下載 IronPrint NuGet 下載 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 列印是應用程式開發的一個基本方面,它允許開發人員透過控制台或實體文件與使用者進行溝通。 在 C# 中, print 語句是用來顯示資訊的通用工具,在本文中,我們將探討它的用法、選項和最佳實務。 C# 列印語句簡介 print 語句用於在 C# 中輸出訊息到控制台。 它促進了程式與使用者之間的溝通,提供了一種顯示訊息、資料或操作結果的方式。 該語句對於調試、使用者互動以及程式執行期間的一般資訊輸出至關重要。 基本文法 C# 中 print 語句的基本語法涉及使用 Console.WriteLine 方法,該方法會自動在指定的字串或值後面新增一行。 Console 類別位於 System 命名空間中,它包含 WriteLine 方法,用於將資訊輸出到標準輸出流。 此方法既可以處理包含多個變數的字串行,也可以處理透過標準輸入流取得的使用者輸入。 舉個簡單的例子: using System; class Program { public static void Main() { // Print a greeting message to the console Console.WriteLine("Hello, C# Print Statement!"); } } using System; class Program { public static void Main() { // Print a greeting message to the console Console.WriteLine("Hello, C# Print Statement!"); } } $vbLabelText $csharpLabel 在這個簡單的範例中,Console 類別的 WriteLine 方法用於將指定的字串列印到控制台,然後換行。 列印變數和值 你可以將字串字面量和變數的數值作為參數包含在 Console.WriteLine 方法中來列印它們。 例如: using System; class Program { public static void Main() { // Define a string message and an integer number string message = "Welcome to C#"; int number = 42; // Print the message and number to the console Console.WriteLine(message); Console.WriteLine("The answer is: " + number); } } using System; class Program { public static void Main() { // Define a string message and an integer number string message = "Welcome to C#"; int number = 42; // Print the message and number to the console Console.WriteLine(message); Console.WriteLine("The answer is: " + number); } } $vbLabelText $csharpLabel 上面的程式碼範例展示如何使用 message 和 number 變數的值透過 WriteLine 方法列印到控制台。 特殊字元和字串格式化 C# 提供了多種使用佔位符或字串插值來格式化輸出的方法。 請查看以下範例: using System; class Program { public static void Main() { // Initialize variables string name = "John"; int age = 30; // Use placeholders for string formatting Console.WriteLine("Name: {0}, Age: {1}", name, age); // Use string interpolation for a cleaner approach Console.WriteLine($"Name: {name}, Age: {age}"); } } using System; class Program { public static void Main() { // Initialize variables string name = "John"; int age = 30; // Use placeholders for string formatting Console.WriteLine("Name: {0}, Age: {1}", name, age); // Use string interpolation for a cleaner approach Console.WriteLine($"Name: {name}, Age: {age}"); } } $vbLabelText $csharpLabel 兩種方法都能達到相同的效果,即允許將變數值插入格式化字串中。 其他格式選項 線路終止器 預設情況下,行終止符為"\r\n"(回車符 + 換行符)。 您可以使用以下方法更改它: Console.Out.NewLine = "\n"; // Set to newline character only Console.Out.NewLine = "\n"; // Set to newline character only $vbLabelText $csharpLabel 自訂格式 格式字串允許使用佔位符和格式選項進行自訂。 例如: using System; class Program { public static void Main() { // Get the current date DateTime currentDate = DateTime.Now; // Print the current date in a long date pattern Console.WriteLine("Today is {0:D}", currentDate); } } using System; class Program { public static void Main() { // Get the current date DateTime currentDate = DateTime.Now; // Print the current date in a long date pattern Console.WriteLine("Today is {0:D}", currentDate); } } $vbLabelText $csharpLabel 複合格式 以下是一個複合格式化並將字元陣列印在一行上的範例: using System; class Program { public static void Main() { // Define a price and character array double price = 19.99; char[] chars = { 'A', 'B', 'C' }; // Format the output string using placeholders Console.WriteLine("Product: {0}, Price: ${1:F2} | Characters: {2}", "Widget", price, new string(chars)); } } using System; class Program { public static void Main() { // Define a price and character array double price = 19.99; char[] chars = { 'A', 'B', 'C' }; // Format the output string using placeholders Console.WriteLine("Product: {0}, Price: ${1:F2} | Characters: {2}", "Widget", price, new string(chars)); } } $vbLabelText $csharpLabel 在這個程式碼範例中,產品名稱和價格使用複合格式進行格式化,字元使用 new string(chars) 以字串形式列印。 新行和換行符 控制新行和換行對於建立輸出至關重要。 Console.WriteLine 方法會自動新增行,但您可以使用 Console.Write 方法在同一行上列印,如下例所示: using System; class Program { public static void Main() { // Print parts of a sentence on the same line Console.Write("This "); Console.Write("is "); Console.Write("on "); Console.WriteLine("the same line."); } } using System; class Program { public static void Main() { // Print parts of a sentence on the same line Console.Write("This "); Console.Write("is "); Console.Write("on "); Console.WriteLine("the same line."); } } $vbLabelText $csharpLabel 上面的程式碼範例會產生如下列印輸出:"這是同一行。 " IronPrint:您的.NET一體化列印庫 IronPrint由Iron Software開發,是一個全面的列印庫,專為.NET開發人員設計,用於列印實體文件。 它提供了廣泛的功能並支援各種環境,使其成為 C# 應用程式中列印文件的多功能解決方案。 如果實體印表機不可用,則使用預設印表機作為預設值來列印文件。 安裝 IronPrint可以透過NuGet程式包管理器控制台或 Visual Studio 套件管理器進行安裝。 若要使用NuGet程式包管理器控制台安裝IronPrint ,請使用下列命令: Install-Package IronPrint 或者,您可以使用 Visual Studio 將其安裝到您的專案中。 右鍵單擊"解決方案資源管理器",然後按一下"管理解決方案的NuGet套件管理器"。 在NuGet瀏覽標籤中,搜尋IronPrint ,然後按一下"安裝"將其新增至您的專案: 為什麼考慮IronPrint? 1. 跨平台魔法 無論您使用的是 Windows、macOS、iOS 還是 Android 系統, IronPrint都能為您提供支援。 它與.NET版本 8、7、6、5 和 Core 3.1+ 相容性良好,使其用途非常廣泛。 2. 格式彈性 從 PDF 到 PNG、HTML、TIFF、GIF、JPEG、IMAGE 和 BITMAP – IronPrint都能處理。 3. 列印設定 允許自訂列印設置,包括 DPI、份數、紙張方向等。 4. 安裝簡便 安裝IronPrint非常簡單。只需使用NuGet套件管理器控制台並輸入命令:Install-Package IronPrint,即可完成安裝。 它是如何運作的? 使用IronPrint進行印刷簡直易如反掌。 看看這個簡單的程式碼範例,您可以輕鬆地使用dialog進行列印並控制列印設定: using IronPrint; class PrintExample { public static void Main() { // Print a document Printer.Print("newDoc.pdf"); // Show a print dialog for user interaction Printer.ShowPrintDialog("newDoc.pdf"); // Customize print settings PrintSettings printSettings = new PrintSettings { Dpi = 150, NumberOfCopies = 2, PaperOrientation = PaperOrientation.Portrait }; // Print using the customized settings Printer.Print("newDoc.pdf", printSettings); } } using IronPrint; class PrintExample { public static void Main() { // Print a document Printer.Print("newDoc.pdf"); // Show a print dialog for user interaction Printer.ShowPrintDialog("newDoc.pdf"); // Customize print settings PrintSettings printSettings = new PrintSettings { Dpi = 150, NumberOfCopies = 2, PaperOrientation = PaperOrientation.Portrait }; // Print using the customized settings Printer.Print("newDoc.pdf", printSettings); } } $vbLabelText $csharpLabel 有關IronPrint及其作為列印中心的功能的更多詳細信息,請訪問文件頁面。 結論 C# 中的 print 語句是與使用者溝通、顯示訊息和偵錯程式碼的強大工具。 無論你是初學者還是經驗豐富的開發者,了解如何有效地使用 Console.WriteLine 方法對於創建資訊豐富且用戶友好的應用程式至關重要。 如果您想要精準、易用且快速的列印庫, IronPrint是您的理想之選。 無論您是在建立 Web 應用程式、使用 MAUI、Avalonia 或任何與 .NET 相關的項目, IronPrint都能為您提供支援。 IronPrint是一個付費圖書館,但提供免費試用。 想讓你的開發者生活更輕鬆一點嗎? 從這裡獲取IronPrint ! 常見問題解答 C# 中列印語句的目的是什麼? 在 C# 中,主要使用 Console.WriteLine 的列印語句用於在控制台上顯示資訊。它在除錯、用戶互動以及向用戶呈現數據或訊息中起到至關重要的作用。 如何使用 Console.WriteLine 列印字串和變數於 C#? 您可以藉由傳遞字串和變數作為參數來使用 Console.WriteLine 列印。例如,Console.WriteLine("數值是 " + variable); 將會列印一個與變數值串聯的字串。 C# 有哪些可用的格式化輸出選項? C# 提供多種格式化選項,包括使用 $"" 語法的字串插值和帶有佔位符的綜合格式化,例如 Console.WriteLine("總計是 {0}", total);。 如何使用 Console.WriteLine 列印特殊字元? 特殊字元可以使用 C# 的跳脫序列列印,例如 \n 代表新行或 \t 代表水平製表符,在傳遞給 Console.WriteLine 的字串中使用。 什麼是 IronPrint,如何使 .NET 開發者受益? IronPrint 是專為 .NET 開發者設計的綜合列印庫,以便於實現實體文件列印。它支持跨平台環境和各種文件格式,提高使用便捷性和不同 .NET 版本的兼容性。 如何將 IronPrint 安裝於專案中使用? IronPrint 可以通過 NuGet 套件管理器安裝,便於整合到您的 .NET 專案中以提升列印功能。 IronPrint 支持哪些列印環境? IronPrint 支持包括 Windows、macOS、iOS 和 Android 在內的多個環境,並兼容 .NET 版本 8、7、6、5 和 Core 3.1+。 如何在 .NET 應用中使用 IronPrint 自訂列印設定? IronPrint 允許通過 PrintSettings 類自訂列印設定,例如 DPI、複製數量和紙張方向,提供對列印過程的靈活控制。 IronPrint 是否提供試用版? 是的,IronPrint 提供免費試用,開發者可以探索其功能和專案內的整合能力。 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年7月28日 如何有效地使用 C# 打印行 在本文中,我們將探索與 C# 打印行有關的各種方法和技術。 閱讀更多 更新2025年6月22日 C# 打印變量:簡化您的代碼 在這篇綜合文章中,我們將探索在 C# 中打印變量的各種方面,涵蓋不同的數據類型,格式化選項以及高級技術。 閱讀更多 C# 打印控制台:逐步指南