IRONSOFTWAREHOME
.NET幫助

C# 列印語句:學習基礎

Jacob Mellor,首席技術官 @ Team Iron
Jacob Mellor
Updated: 2026年4月21日

列印是應用程式開發的重要方面,允許開發者通過控制台或實體文件與使用者溝通。 在C#中,列印語句是一個多功能的工具用於顯示訊息,本文將探討其用法、選項和最佳實踐。

C#列印語句介紹

print語句用於在C#中將訊息輸出到控制台。 它促進了程式與使用者之間的通訊,提供了一種顯示訊息、資料或操作結果的方法。 該語句對於偵錯、使用者互動及程式執行期間的一般訊息輸出至關重要。

基本語法

在C#中,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!");
    }
}

在這個簡單的例子中,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);
    }
}

這裡上面的程式碼範例展示了如何使用number變數的值列印到控制台。

C#列印語句(如何對開發者運作):圖1 - Console.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}");
    }
}

兩種方法都達成相同的結果,允許您在格式化的字串中插入變數值。

額外的格式化選項

行終止符

預設情況下,行終止符是 "\r\n" (回車+換行)。 您可以使用以下方法更改它:

Console.Out.NewLine = "\n";
// Set to newline character only

自定義格式化

格式字串允許使用佔位符和格式選項進行自定義。 例如:

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()
    {
        // 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));
    }
}

在這個程式碼範例中,產品名稱和價格都是使用合成格式化,並使用new string(chars)將字元列印為字串。

新行和換行

控制新行和換行對於結構化輸出至關重要。 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.");
    }
}

上述程式碼例子產生的列印輸出:"這是在同一行。"

IronPrint:您的.NET一站式列印程式庫

IronPrint由Iron Software開發,是一個設計給.NET開發者用於列印實體文件的綜合列印程式庫。 它提供了多種功能並支援多個環境,使其成為C#應用程式中文件列印的靈活解決方案。 如果實體列印機不可用,它將使用預設列印機作為其列印文件的預設值。

C#列印語句(如何對開發者運作):圖2 - IronPrint for .NET:C#列印程式庫

安裝

IronPrint可以通過NuGet套件管理器控制台或使用Visual Studio套件管理器安裝。

要使用NuGet套件管理器控制台安裝IronPrint,請使用以下命令:

PM > Install-Package IronPrint

或者,您可以使用Visual Studio將其安裝到專案中。 右鍵點擊Solution Explorer,然後點擊管理NuGet套件管理器以管理解決方案。 在NuGet的瀏覽標籤中,搜尋IronPrint,然後點擊安裝將其新增到您的專案中:

C# Print Statement (How It Works For Developers): Figure 3 - Install IronPrint using NuGet Package Manager by searching "IronPrint" in the search bar of NuGet Package Manager, then selecting the project and clicking the Install button.

為什麼考慮IronPrint?

1. 跨平台魔力

無論您是在Windows、macOS、iOS還是Android工作,IronPrint都能支援。 它與.NET 版本8、7、6、5及Core 3.1+良好運行,令其變得非常靈活。

2. 格式靈活性

IronPrint可處理從PDF到PNG、HTML、TIFF、GIF、JPEG、IMAGE及BITMAP。

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);
    }
}

如需關於IronPrint及其作為列印樞紐功能的詳細資訊,請存取說明檔案頁面

結論

C#中的print語句是與使用者溝通、顯示訊息和偵錯程式碼的強大工具。 無論您是一名初學者還是經驗豐富的開發人員,了解如何有效使用Console.WriteLine方法是建立資訊豐富和使用者友好型應用程式的關鍵。

IronPrint是您想要準確性、使用簡便性和速度的列印程式庫。 無論您是在建立WebApps、使用MAUI、Avalonia或任何與.NET相關的工作,IronPrint都能支援您。

IronPrint是一個付費程式庫,但有提供免費試用

準備好讓您的開發者生活變得更輕鬆嗎? 從這裡獲取IronPrint

Jacob Mellor,首席技術官 @ Team Iron
首席技術官

Jacob Mellor是Iron Software的首席技術官,一位在C# PDF技術上開創先河的遠見工程師。作為Iron Software核心程式碼庫的原開發者,他從創立以來就一直在塑造公司的產品架構,與首席執行官Cameron Rimington一起將公司轉變為服務於NASA、特斯拉和全球政府公司的50多名人員的公司。

...
閱讀更多

相關文章

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預約您的免費
現場演示
Booking Badge

全球數百萬工程師信賴

Iron Software的客戶標誌
獲得非義務性諮詢
完成下列表單或發郵件到sales@ironsoftware.com
您的資訊將始終保密。
全球數百萬工程師信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立