在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
打印是应用程序开发的一个基本方面,允许开发人员通过控制台或实体文档与用户交流。 在C#中,打印语句是一个用于显示信息的多功能工具,在本文中,我们将探讨其用法、选项和最佳实践。
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
在这个简单的例子中,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
这里上述代码示例展示了如何使用WriteLine方法将message和number变量的值打印到控制台。
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
这两种方法都能达到相同的效果,即在格式化字符串中插入变量值。
默认情况下,行终止符是“\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
格式字符串允许自定义占位符和格式选项。 例如:
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)
下面是在一行中合成格式并打印字符数组的示例:
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))
在此代码示例中,产品名称和价格使用复合格式化,字符使用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
上面的代码示例生成打印输出:"这在同一行上。"
IronPrint,由Iron Software开发,是一个全面的打印库,专为.NET开发人员打印实体文档而设计。 它提供广泛的功能并支持各种环境,是 C# 应用程序中打印文档的多功能解决方案。 如果没有物理打印机,则使用默认打印机作为打印文档的默认值。
IronPrint 可以通过 NuGet 包管理控制台或使用 Visual Studio 包管理器安装。
要使用 NuGet 软件包管理器控制台安装 IronPrint,请使用以下命令:
Install-Package IronPrint
您也可以使用 Visual Studio 将其安装到您的项目中。 右键单击 "解决方案资源管理器",然后单击 "管理解决方案的 NuGet 包管理器"。 在NuGet浏览选项卡中,搜索IronPrint,然后点击安装将其添加到您的项目中:
无论您是在 Windows、macOS、iOS 还是 Android 上工作,IronPrint 都会为您提供支持。 它能很好地与 .NET 8、7、6、5 和 Core 3.1+ 版本配合使用,通用性极强。
从 PDF 到 PNG、HTML、TIFF、GIF、JPEG、IMAGE 和 BITMAP - IronPrint 都能处理。
允许自定义打印设置,包括 DPI、份数、纸张方向等。
安装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)
有关IronPrint及其作为打印中心的功能的更多详细信息,请访问文档页面。
C# 中的 print 语句是一个用于与用户交流、显示信息和调试代码的强大工具。 无论您是初学者还是经验丰富的开发人员,了解如何有效使用Console.WriteLine方法对于创建信息丰富且用户友好的应用程序至关重要。
IronPrint 是您在需要打印准确性、易用性和速度时的首选打印库。 无论您是在构建 WebApps、使用 MAUI、Avalonia,还是任何与 .NET 相关的内容,IronPrint 都能帮助您。
IronPrint 是一个付费库,但提供 免费试用 页面。
准备好让您的开发生活更轻松了吗? 从此处获取IronPrint!