IRONSOFTWAREHOME
.NET 帮助

C# 打印语句:学习基础知识

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

打印是应用程序开发的一个基本方面,它允许开发人员通过控制台或物理文档与用户进行沟通。 在 C# 中, print 语句是一个用于显示信息的通用工具,在本文中,我们将探讨它的用法、选项和最佳实践。

C# 打印语句简介

在C#中,print语句用于向控制台输出信息。 它促进了程序与用户之间的沟通,提供了一种显示消息、数据或操作结果的方式。 该语句对于调试、用户交互以及程序执行期间的一般信息输出至关重要。

基本语法

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 将其安装到您的项目中。 右键单击"解决方案资源管理器",然后单击"管理解决方案的 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. 格式灵活性

从 PDF 到 PNG、HTML、TIFF、GIF、JPEG、IMAGE 和 BITMAP – IronPrint 都能处理。

3. 打印设置

允许自定义打印设置,包括 DPI、份数、纸张方向等。

4. 安装简便

安装IronPrint非常简单。只需使用NuGet包管理器控制台并键入命令:Install-Package IronPrint,您就可以开始使用了。

它是如何运作的?

using 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是您的理想之选。 无论您是在构建 Web 应用程序、使用 MAUI、Avalonia 还是任何与 .NET 相关的项目, IronPrint都能为您提供支持。

IronPrint是一个付费库,但提供免费试用

想让你的开发者生活更轻松一些吗? 从这里获取IronPrint

Jacob Mellor,Team Iron 的首席技术官
首席技术官

Jacob Mellor 是 Iron Software 的首席技术官,也是一位开创 C# PDF 技术的有远见的工程师。作为 Iron Software 核心代码库的原始开发者,他从公司成立之初就开始塑造公司的产品架构,与首席执行官 Cameron Rimington 一起将公司转变为一家拥有 50 多名员工的公司,为 NASA、特斯拉和全球政府机构提供服务。

相关文章

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 天试用密钥
无需信用卡或创建账户