IRONSOFTWAREHOME
開發者更新

C# 運算子 (對開發者如何運作)

Jacob Mellor, Chief Technology Officer @ Team Iron
Jacob Mellor
Updated: 2026年4月21日

在 C# 中,運算子在對變數和值進行各種操作中起著至關重要的作用。 無論您是初學者還是有經驗的開發者,牢固理解C# 運算子是編寫高效且表現力豐富的程式碼的基礎。 在本綜合指南中,我們將探索 C# 中不同型別的運算子及其在您的程式中如何使用。 我們還將了解如何將這些 C# 運算子與 IronPDF 一起使用。

1. Types of Operators in C#

1.1. 算術運算子

C# 中的算術運算子用於基本的數學運算。 這些包括加法 (+)、減法 (-)、乘法 (*)、除法 (/) 和餘數 (%)。 對於算術運算子,運算子的優先權類似於數學運算子優先權中常見的 BEDMAS 或 PEDMAS。

讓我們深入了解一個例子來理解這些運算子如何工作:

// Arithmetic Operators
int a = 10;
int b = 3;
int sum = a + b; // Adds the values of a and b
int difference = a - b; // Subtracts b from a
int product = a * b; // Multiplies a by b
int quotient = a / b; // Divides a by b (integer division)
int remainder = a % b; // Modulus operation; gives the remainder of a divided by b

Console.WriteLine("Arithmetic Operators:");
Console.WriteLine($"Sum: {sum}, Difference: {difference}, Product: {product}, Quotient: {quotient}, Remainder: {remainder}");
Console.WriteLine();

1.2. 關係運算子

關係運算子用於比較值並確定它們之間的關係。 C# 中常見的關係運算子包括大於 (>)、小於 (<)、等於 (==)、不等於 (!=)、大於或等於 (>=) 和小於或等於 (<=)。

// Relational Operators
bool isEqual = (a == b); // Checks if a is equal to b
bool notEqual = (a != b); // Checks if a is not equal to b
bool greaterThan = (a > b); // Checks if a is greater than b
bool lessThan = (a < b); // Checks if a is less than b
bool greaterOrEqual = (a >= b); // Checks if a is greater than or equal to b
bool lessOrEqual = (a <= b); // Checks if a is less than or equal to b

Console.WriteLine("Relational Operators:");
Console.WriteLine($"Equal: {isEqual}, Not Equal: {notEqual}, Greater Than: {greaterThan}, Less Than: {lessThan}, Greater or Equal: {greaterOrEqual}, Less or Equal: {lessOrEqual}");
Console.WriteLine();

1.3. 邏輯運算子

邏輯運算子用於對布林值進行邏輯運算。 C#中常見的邏輯運算是 AND (&&)、OR (||), and NOT (!)。 AND 和 OR 是二元運算子,有兩個運算元; 然而,NOT 是一元運算子,這意味著它只影響一個運算元。

// Logical Operators
bool condition1 = true;
bool condition2 = false;
bool resultAnd = condition1 && condition2; // true if both conditions are true
bool resultOr = condition1 || condition2; // true if either condition is true
bool resultNot = !condition1; // inverts the Boolean value of condition1

Console.WriteLine("Logical Operators:");
Console.WriteLine($"AND: {resultAnd}, OR: {resultOr}, NOT: {resultNot}");
Console.WriteLine();

1.4. 賦值運算子

賦值運算子用於將值賦給變數。 簡單的賦值運算子是 =。 然而,C#也提供了複合賦值運算子,如 /=%=

// Assignment Operators
int x = 5; // Assigns 5 to x
int y = 2; // Assigns 2 to y
x += y; // Increases x by the value of y
y *= 3; // Multiplies y by 3

Console.WriteLine("Assignment Operators:");
Console.WriteLine($"x after +=: {x}, y after *=: {y}");
Console.WriteLine();

1.5. 位運算運算子

位運算運算子在位級別執行操作。 常見的位運算運算子包括位 AND (&)、位 OR (|), and right shift (>>)。

// Bitwise Operators
int p = 5; // Binary: 0101
int q = 3; // Binary: 0011
int bitwiseAnd = p & q; // Binary AND operation
int bitwiseOr = p | q; // Binary OR operation
int bitwiseXor = p ^ q; // Binary XOR operation
int bitwiseNotP = ~p; // Binary NOT operation (complement)
int leftShift = p << 1; // Shift bits of p left by 1
int rightShift = p >> 1; // Shift bits of p right by 1

Console.WriteLine("Bitwise Operators:");
Console.WriteLine($"AND: {bitwiseAnd}, OR: {bitwiseOr}, XOR: {bitwiseXor}, NOT: {bitwiseNotP}, Left Shift: {leftShift}, Right Shift: {rightShift}");
Console.WriteLine();

1.6. 條件運算子(三元運算子)

條件運算子 (?:) 是在一行中表達 if-else 聲明的簡寫方式。

// Conditional (Ternary) Operator
int age = 20;
string result = (age >= 18) ? "Adult" : "Minor"; // Checks if age is 18 or more

Console.WriteLine("Conditional Operator:");
Console.WriteLine($"Result: {result}");
Console.WriteLine();

在此範例中,result 的值將是"成人"如果 age 大於或等於 18,否則為"未成年"。

1.7. 空合併運算子

空合併運算子 (??) 用於為可空型別提供預設值。

// Null-Coalescing Operator
int? nullableValue = null;
int resultCoalesce = nullableValue ?? 10; // Uses value 10 if nullableValue is null

Console.WriteLine("Null-Coalescing Operator:");
Console.WriteLine($"Result: {resultCoalesce}");

1.8. 所有 C# 運算子程式碼範例的輸出截圖

C# 運算子(對開發者的作用):圖1 - 所有運算子的輸出。

2. 介紹 IronPDF

IronPDF for C# 是一個多功能程式庫,讓開發者能夠無縫地將 PDF 相關功能整合到他們的 .NET 應用程式中。 提供一套完整的工具,IronPDF 促進從 PDF 文件中建立、修改和提取資訊的過程。 無論是從 HTML 生成動態 PDF、捕捉網站內容,還是執行高級格式化,IronPDF 都透過直觀的 API 簡化這些過程。

IronPDF 在需要 PDF 操作的應用中廣泛使用,例如報告生成和文件管理系統。 IronPDF 簡化了複雜的任務,使其成為使用 C# 和 .NET 技術的開發者的有價值資源。 始終查閱官方文件以獲得精確的使用說明和更新。

2.1. 開始使用 IronPDF

要在您的 C# 專案中開始使用 IronPDF,您首先需要安裝 IronPDF NuGet package。 您可以通過套件管理器控制台使用以下命令來執行此操作:

PM > Install-Package IronPdf

或者,您可以使用 NuGet 套件管理器搜尋"IronPDF"並從那裡安裝套件。

一旦安裝套件,您便可以開始使用 IronPDF 來無縫地處理 PDF 檔案。

2.2. 程式碼範例:使用 C# 運算子與 IronPDF

using IronPdf;
using System;
class Program
{
    static void Main()
    {
        // Create an instance of ChromePdfRenderer
        var renderer = new ChromePdfRenderer();
        
        // Add HTML content with mathematical operations
        string content = $@"<!DOCTYPE html>
                            <html>
                            <body>
                                <h1>Mathematical Operations in IronPDF</h1>
                                <p>Sum: 5 + 7 = {5 + 7}</p>
                                <p>Product: 3 * 4 = {3 * 4}</p>
                                <p>Division: 10 / 2 = {10 / 2}</p>
                                <p>Modulus: 15 % 4 = {15 % 4}</p>
                            </body>
                            </html>";

        // Render HTML content to PDF
        var pdf = renderer.RenderHtmlAsPdf(content);

        // Save the PDF to a file
        pdf.SaveAs("MathOperations.pdf");
        Console.WriteLine("PDF with mathematical operations created successfully!");
    }
}

此 C# 程式碼利用 IronPDF 程式庫建立了一個包含我們所展示的多個運算子的 PDF 文件。 它使用 ChromePdfRenderer 類別來渲染 HTML 內容,其中包括使用 C# 運算子計算的數學表達式。

HTML 內容中包含的標題和段落顯示了如和、乘積、除法和餘數等結果,並透過字串格式化進行插值。 然後,渲染的 HTML 使用 IronPDF 轉換為 PDF,並將生成的 PDF 保存為"MathOperations.pdf"。

C# 運算子(對開發者的作用):圖2 - 前面程式碼產生的PDF文件

3. 結論

掌握 C# 運算子對於開發者來說是至關重要的,能夠透過算術、關係、邏輯、賦值、位運算、條件和空合併運算實現高效編碼。 本綜合指南探索了各種運算子的型別,並提供了實用的程式碼範例。 此外,介紹了 IronPDF,突出了其在 C# 中執行PDF相關任務的實用性。

通過無縫整合 C# 運算子與 IronPDF,開發者可以輕鬆在 PDF 文件中執行算術運算,展示此程式庫的多功能性。 總體來說,對 C# 運算子的深入理解使開發者能夠為多種程式任務建立更強大和表現力豐富的程式碼。

您可以通過存取此連結獲取 IronPDF 的免費試用授權。 要了解有關 IronPDF 的更多資訊,請存取這裡,有關程式碼範例,請存取這裡

Jacob Mellor, Chief Technology Officer @ Team Iron
Chief Technology Officer

Jacob Mellor is Chief Technology Officer at Iron Software and a visionary engineer pioneering C# PDF technology. As the original developer behind Iron Software's core codebase, he has shaped the company's product architecture since its inception, transforming it alongside CEO Cameron Rimington into a 50+ person company serving NASA, Tesla, and global government agencies.

...
Read More

Related Articles

Key in blue circle

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

bullet_checked無需信用卡或建立帳號
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預訂您的免費現場演示
Booking Badge related to IronPDF Product Demo

受到全球數百萬工程師的信任

Iron Software的客戶標誌
獲取您的無義務諮詢
填寫以下表格或電子郵件sales@ironsoftware.com
您的詳細資訊將始終保密
受到全球數百萬工程師的信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立