.NET 帮助

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

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

C# 和 num 简介;打印语句

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

基本语法

C# 中 print 语句的基本语法涉及使用 Console.WriteLine 方法,该方法会在指定的字符串或值后自动添加一个新行。 Console 类位于 System 名称空间中,包含 WriteLine 方法,用于向标准输出流输出信息。 这种方法既可以使用具有多个变量的字符串行,也可以使用通过标准输入流获取的用户输入。

下面是一个简单的例子:

using System;
class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello, C# Print Statement!");
    }
}
using System;
class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello, C# Print Statement!");
    }
}
Imports System
Friend Class Program
	Public Shared Sub Main()
		Console.WriteLine("Hello, C# Print Statement!")
	End Sub
End Class
$vbLabelText   $csharpLabel

在这个简单的例子中,Console类的WriteLine方法被用来将指定的字符串打印到控制台,随后是一个新行。

打印变量和值

您可以通过在Console.WriteLine方法中将它们作为参数来打印字符串字面量和变量的数值。 例如

using System;
class Program
{
    public static void Main()
    {
        string message = "Welcome to C#";
        int number = 42;
        Console.WriteLine(message);
        Console.WriteLine("The answer is: " + number);
    }
}
using System;
class Program
{
    public static void Main()
    {
        string message = "Welcome to C#";
        int number = 42;
        Console.WriteLine(message);
        Console.WriteLine("The answer is: " + number);
    }
}
Imports System
Friend Class Program
	Public Shared Sub Main()
		Dim message As String = "Welcome to C#"
		Dim number As Integer = 42
		Console.WriteLine(message)
		Console.WriteLine("The answer is: " & number)
	End Sub
End Class
$vbLabelText   $csharpLabel

这里上述代码示例展示了如何使用WriteLine方法将messagenumber变量的值打印到控制台。

C# 打印语句(它如何为开发者工作):图1 - Console.WriteLine 输出

特殊字符和字符串格式化

C# 提供了多种使用占位符或字符串插值来格式化输出的方法。 请查看以下示例:

using System;
class Program
{
    public static void Main()
    {
        string name = "John";
        int age = 30;
        Console.WriteLine("Name: {0}, Age: {1}", name, age);
        Console.WriteLine($"Name: {name}, Age: {age}");
    }
}
using System;
class Program
{
    public static void Main()
    {
        string name = "John";
        int age = 30;
        Console.WriteLine("Name: {0}, Age: {1}", name, age);
        Console.WriteLine($"Name: {name}, Age: {age}");
    }
}
Imports System
Friend Class Program
	Public Shared Sub Main()
		Dim name As String = "John"
		Dim age As Integer = 30
		Console.WriteLine("Name: {0}, Age: {1}", name, age)
		Console.WriteLine($"Name: {name}, Age: {age}")
	End Sub
End Class
$vbLabelText   $csharpLabel

这两种方法都能达到相同的效果,即在格式化字符串中插入变量值。

其他格式选项

行终结者

默认情况下,行终止符是“\r\n”(回车 + 换行)。 您可以使用

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

Console.Out.NewLine = vbLf
' Set to newline character only
$vbLabelText   $csharpLabel

自定义格式

格式字符串允许自定义占位符和格式选项。 例如:

DateTime currentDate = DateTime.Now;
Console.WriteLine("Today is {0:D}", currentDate);
DateTime currentDate = DateTime.Now;
Console.WriteLine("Today is {0:D}", currentDate);
Dim currentDate As DateTime = DateTime.Now
Console.WriteLine("Today is {0:D}", currentDate)
$vbLabelText   $csharpLabel

复合格式

下面是在一行中合成格式并打印字符数组的示例:

double price = 19.99;
char [] chars = { 'A', 'B', 'C' };
Console.WriteLine("Product: {0}, Price: ${1:F2} 
 Characters: {2}", "Widget", price, new string(chars));
double price = 19.99;
char [] chars = { 'A', 'B', 'C' };
Console.WriteLine("Product: {0}, Price: ${1:F2} 
 Characters: {2}", "Widget", price, new string(chars));
Dim price As Double = 19.99
Dim chars() As Char = { "A"c, "B"c, "C"c }
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()
    {
        Console.Write("This ");
        Console.Write("is ");
        Console.Write("on ");
        Console.WriteLine("the same line.");
    }
}
using System;
class Program
{
    public static void Main()
    {
        Console.Write("This ");
        Console.Write("is ");
        Console.Write("on ");
        Console.WriteLine("the same line.");
    }
}
Imports System
Friend Class Program
	Public Shared Sub Main()
		Console.Write("This ")
		Console.Write("is ")
		Console.Write("on ")
		Console.WriteLine("the same line.")
	End Sub
End Class
$vbLabelText   $csharpLabel

上面的代码示例生成打印输出:"这在同一行上。"

IronPrint:您的.NET全能打印库

IronPrint,由Iron Software开发,是一个全面的打印库,专为.NET开发人员打印实体文档而设计。 它提供广泛的功能并支持各种环境,是 C# 应用程序中打印文档的多功能解决方案。 如果没有物理打印机,则使用默认打印机作为打印文档的默认值。

C# 打印语句(开发人员如何使用):图 2 - IronPrint for .NET:C# 打印库

安装

IronPrint 可以通过 NuGet 包管理控制台或使用 Visual Studio 包管理器安装。

要使用 NuGet 软件包管理器控制台安装 IronPrint,请使用以下命令:

Install-Package IronPrint

您也可以使用 Visual Studio 将其安装到您的项目中。 右键单击 "解决方案资源管理器",然后单击 "管理解决方案的 NuGet 包管理器"。 在NuGet浏览选项卡中,搜索IronPrint,然后点击安装将其添加到您的项目中:

C# Print Statement(开发人员如何使用):图 3 - 在 NuGet 包管理器的搜索栏中搜索"ironprint",然后选择项目并点击安装按钮,以通过 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 就像在公园散步。 请看下面这个快速的代码示例,您可以轻松地使用对话框进行打印,并控制打印设置

using IronPrint;
// Print a document
Printer.Print("newDoc.pdf");
// Show a print dialog
Printer.ShowPrintDialog("newDoc.pdf");
// Customize print settings
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 150;
printSettings.NumberOfCopies = 2;
printSettings.PaperOrientation = PaperOrientation.Portrait;
Printer.Print("newDoc.pdf", printSettings);
using IronPrint;
// Print a document
Printer.Print("newDoc.pdf");
// Show a print dialog
Printer.ShowPrintDialog("newDoc.pdf");
// Customize print settings
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 150;
printSettings.NumberOfCopies = 2;
printSettings.PaperOrientation = PaperOrientation.Portrait;
Printer.Print("newDoc.pdf", printSettings);
Imports IronPrint
' Print a document
Printer.Print("newDoc.pdf")
' Show a print dialog
Printer.ShowPrintDialog("newDoc.pdf")
' Customize print settings
Dim printSettings As New PrintSettings()
printSettings.Dpi = 150
printSettings.NumberOfCopies = 2
printSettings.PaperOrientation = PaperOrientation.Portrait
Printer.Print("newDoc.pdf", printSettings)
$vbLabelText   $csharpLabel

有关IronPrint及其作为打印中心的功能的更多详细信息,请访问文档页面

结论

C# 中的 print 语句是一个用于与用户交流、显示信息和调试代码的强大工具。 无论您是初学者还是经验丰富的开发人员,了解如何有效使用Console.WriteLine方法对于创建信息丰富且用户友好的应用程序至关重要。

IronPrint 是您在需要打印准确性、易用性和速度时的首选打印库。 无论您是在构建 WebApps、使用 MAUI、Avalonia,还是任何与 .NET 相关的内容,IronPrint 都能帮助您。

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

准备好让您的开发生活更轻松了吗? 从此处获取IronPrint

查克尼特·宾
软件工程师
Chaknith 负责 IronXL 和 IronBarcode 的工作。他在 C# 和 .NET 方面拥有深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的洞察力,有助于提升产品、文档和整体体验。
< 前一页
C# 打印控制台:逐步指南

准备开始了吗? 版本: 2025.5 刚刚发布

查看许可证 >