在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
在 C# 中打印行是控制台應用程式的一個基本方面,它涉及在控制台螢幕上顯示文字或指定值。 無論您是使用標準輸出流還是格式化字串,了解如何在 C# 主控台應用程式中高效地打印行是至關重要的。
在本文中,我們將探討有關在 C# 中列印行的各種方法和技術。
在 C# 中,印出一行通常涉及使用Console.WriteLine方法。 我們先來看一個簡單的例子:
using System;
class Program {
public static void Main() {
Console.WriteLine("Hello, C# Print Line!");
}
}
using System;
class Program {
public static void Main() {
Console.WriteLine("Hello, C# Print Line!");
}
}
Imports System
Friend Class Program
Public Shared Sub Main()
Console.WriteLine("Hello, C# Print Line!")
End Sub
End Class
在上述程式碼中,Console.WriteLine 陳述式會輸出指定的字串值("Hello, C# Print Line!"),並在後面加上一個新行。這是透過 WriteLine 方法實現的,它在輸出結尾附加了一個行終止符。
行終止符是一個特殊字符或序列,用於指示一行的結束。兩個最常見的行終止符是回車符('\r')和換行符('\n')。 在 C# 中,Console.WriteLine 方法負責根據操作系統使用適當的當前行終止符。
public static void Main() {
Console.WriteLine("This is on the first line.");
Console.WriteLine("This is on the second line.");
}
public static void Main() {
Console.WriteLine("This is on the first line.");
Console.WriteLine("This is on the second line.");
}
Public Shared Sub Main()
Console.WriteLine("This is on the first line.")
Console.WriteLine("This is on the second line.")
End Sub
在上述範例中,程式執行後,每個 Console.WriteLine 都會在 C# 主控台視窗中產生新的一行,結果是兩行指定的內容。
如果你需要明確控制行終止符,可以使用Console.Write方法並手動添加所需的行終止符:
public static void Main() {
Console.Write("This is on the first line.");
Console.Write('\r'); // Carriage return
Console.Write("This is on the same line but very far left position.");
}
public static void Main() {
Console.Write("This is on the first line.");
Console.Write('\r'); // Carriage return
Console.Write("This is on the same line but very far left position.");
}
Imports Microsoft.VisualBasic
Public Shared Sub Main()
Console.Write("This is on the first line.")
Console.Write(ControlChars.Cr) ' Carriage return
Console.Write("This is on the same line but very far left position.")
End Sub
在此範例中,使用回車符號('\r')將光標置於行首,導致文字的第二部分出現在最左側位置,即覆蓋前面的輸出。
要打印多行而不重複使用Console.WriteLine語句,您可以使用可變長度參數列表:
public static void Main() {
PrintLines("Line 1", "Line 2", "Line 3");
}
static void PrintLines(params string [] lines) {
foreach (var line in lines) {
Console.WriteLine(line);
}
}
public static void Main() {
PrintLines("Line 1", "Line 2", "Line 3");
}
static void PrintLines(params string [] lines) {
foreach (var line in lines) {
Console.WriteLine(line);
}
}
Public Shared Sub Main()
PrintLines("Line 1", "Line 2", "Line 3")
End Sub
Shared Sub PrintLines(ParamArray ByVal lines() As String)
For Each line In lines
Console.WriteLine(line)
Next line
End Sub
PrintLines 方法是我們創建的,接受指定的字串參數數組,使您能夠傳遞任意數量的新行以列印指定的字串值:
格式輸出至關重要,特別是在處理不同數據類型時。Console.WriteLine 方法提供了幾個重載,可接受指定的物件與格式資訊:
public static void Main() {
int answer = 42;
string name = "John Doe";
Console.WriteLine("The answer is {0}.", answer);
Console.WriteLine("Hello, {0}!", name);
}
public static void Main() {
int answer = 42;
string name = "John Doe";
Console.WriteLine("The answer is {0}.", answer);
Console.WriteLine("Hello, {0}!", name);
}
Public Shared Sub Main()
Dim answer As Integer = 42
Dim name As String = "John Doe"
Console.WriteLine("The answer is {0}.", answer)
Console.WriteLine("Hello, {0}!", name)
End Sub
在這個範例中,{0} 是指定對象的佔位符(在此情況下,answer 和 name),允許您在輸出中包含可變數據並列印指定格式的信息。
對於特殊換行或 Unicode 字元,您可以使用轉義序列。 您也可以使用 Console.WriteLine 列印任何 ASCII 字面值或有效的 HTML 代碼。例如,要在同一字符串中加入單行斷行:
public static void Main() {
Console.WriteLine("This is line 1.\nThis is line 2.");
Console.WriteLine("Line 1\u000Aline 2");
}
public static void Main() {
Console.WriteLine("This is line 1.\nThis is line 2.");
Console.WriteLine("Line 1\u000Aline 2");
}
Imports Microsoft.VisualBasic
Public Shared Sub Main()
Console.WriteLine("This is line 1." & vbLf & "This is line 2.")
Console.WriteLine("Line 1" & vbLf & "line 2")
End Sub
在這裡,\n 和 \u000A, 這些指定的 Unicode 字符,皆表示一個換行字符,在每種情況下都會使文本移動到下一行。
以下程式碼在 Console.WriteLine 方法中使用了字串插值。 字串插值是 C# 6.0 中引入的一個功能,它簡化了在字串文本中嵌入表達式或變數的過程,並且在主控台應用程式上正確顯示指定的布林值。
Random rnd = new Random();
for (int i = 1; i <= 5; i++)
{
bool isTrue = rnd.Next(0, 2) == 1;
Console.WriteLine($"True or False: {isTrue}");
}
Random rnd = new Random();
for (int i = 1; i <= 5; i++)
{
bool isTrue = rnd.Next(0, 2) == 1;
Console.WriteLine($"True or False: {isTrue}");
}
Dim rnd As New Random()
For i As Integer = 1 To 5
Dim isTrue As Boolean = rnd.Next(0, 2) = 1
Console.WriteLine($"True or False: {isTrue}")
Next i
從表達式返回的指定數據如下面所示,打印在控制台應用程序上:
在程式設計中,列印各種數字格式是一個常見的需求,特別是在處理雙精度浮點數和單精度浮點數時。 同樣地,可以使用Console.WriteLine語句來準確且輕鬆地列印它們。
double doubleValue = 0.123456789;
Console.WriteLine($"Double Precision: {doubleValue:F7}");
double doubleValue = 0.123456789;
Console.WriteLine($"Double Precision: {doubleValue:F7}");
Dim doubleValue As Double = 0.123456789
Console.WriteLine($"Double Precision: {doubleValue:F7}")
在此範例中,F7 指定雙精度值應以小數點後 7 位數格式化。 您可以調整「F」後的數字來控制精度。
string existingString = "Hello, C#!";
Console.WriteLine($"Existing String: {existingString}");
string existingString = "Hello, C#!";
Console.WriteLine($"Existing String: {existingString}");
Dim existingString As String = "Hello, C#!"
Console.WriteLine($"Existing String: {existingString}")
列印現有字串很簡單。 只需使用Console.WriteLine,然後包含您想顯示的字串。
float singleValue = 0.123456789f;
Console.WriteLine($"Single Precision: {singleValue:F7}");
float singleValue = 0.123456789f;
Console.WriteLine($"Single Precision: {singleValue:F7}");
Dim singleValue As Single = 0.123456789F
Console.WriteLine($"Single Precision: {singleValue:F7}")
類似於雙精度,這裡對於單精度浮點數使用F7格式規範。 您可以根據您的精確度要求調整 'F' 後面的數字。
打印文件是許多應用程式的重要部分,而在充分利用 C# 中的打印潛能方面,IronPrint 脫穎而出,成為一個多功能且功能豐富的函式庫。
IronPrint,由Iron Software開發,是一個為.NET生態系統(包括C#)設計的先進列印庫。 無論您是在桌面、行動或網頁應用程式上工作,IronPrint 能夠無縫整合到您的 C# 專案中,提供一套全面的工具來應對各種列印需求。
1. 跨平台兼容性:
IronPrint 支援多種作業系統,包括 Windows、macOS、Android 和 iOS。 此跨平台相容性確保您的列印解決方案能夠到達不同環境的用戶。
2. .NET 版本支援:
與 .NET Framework 4.6.2 及以上版本、.NET Core 3.1+ 和最新 .NET 版本兼容,IronPrint 覆蓋了廣泛的 .NET 環境。
3. 專案類型支持:
IronPrint 適用於不同類型的專案,包括行動裝置(Xamarin 和 MAUI)、桌面(WPF 和 MAUI)和主控台(應用程式和程式庫)。 這種靈活性使其適用於各種應用程式架構。
4. 簡單安裝:
開始使用IronPrint非常輕鬆。 您可以透過使用 NuGet 套件管理器主控台並執行 Install-Package IronPrint 命令來快速安裝該庫。
下面是一個簡單的範例,展示了在 C# 控制台應用程式中使用 IronPrint 來列印文件有多麼容易:
using IronPrint;
class Program
{
static void Main()
{
Console.WriteLine("Printing Started...");
// Silent printing of a document
Printer.Print("document.pdf");
// Or display a print dialog
Printer.ShowPrintDialog("document.pdf");
Console.WriteLine("Printing Completed...");
}
}
using IronPrint;
class Program
{
static void Main()
{
Console.WriteLine("Printing Started...");
// Silent printing of a document
Printer.Print("document.pdf");
// Or display a print dialog
Printer.ShowPrintDialog("document.pdf");
Console.WriteLine("Printing Completed...");
}
}
Imports IronPrint
Friend Class Program
Shared Sub Main()
Console.WriteLine("Printing Started...")
' Silent printing of a document
Printer.Print("document.pdf")
' Or display a print dialog
Printer.ShowPrintDialog("document.pdf")
Console.WriteLine("Printing Completed...")
End Sub
End Class
此處的輸出顯示使用 Print 和 ShowPrintDialog 方法列印文件。 如果未安裝實體打印機,則使用預設打印機進行打印。
IronPrint 超越了基本的印刷任務,並提供以下高級功能:
IronPrint 允許您為不同平台量身打造您的列印解決方案。 例如,在處理針對特定平台(如 Windows、Android、iOS 或 macOS)的 .NET Core 專案時,您可以相應地調整專案檔中的 TargetFrameworks 屬性。
要獲得有關 IronPrint 的更多詳細資訊,請訪問此文件和API 參考頁面。
在 C# 中打印行是控制檯應用程式開發的基礎技能。 無論是顯示文本、格式化輸出還是控制行終止符,了解可用的各種技術將提高您創建有效且易讀的控制台程序的能力。 探索Console類別提供的多種方法,嘗試不同的格式選項,並利用C#的靈活性,在應用程式中生成清晰且結構良好的控制台輸出。
IronPrint 作為尋求強大且靈活列印功能的 C# 開發人員的有力盟友出現。 憑藉其跨平台支持、與各種 .NET 版本的兼容性,以及先進的打印功能,IronPrint 簡化了在各種 C# 應用程式中實現打印解決方案的過程。 無論您是在為桌面、行動裝置還是網頁開發,IronPrint 都提供了您所需的工具,使您的打印需求在 C# 開發世界中得以實現。