.NET ヘルプ

C# 演算子(開発者にとっての動作方法)

更新済み 3月 6, 2024
共有:

C#において、演算子は変数や値に対して様々な操作を行う上で重要な役割を果たします。 初心者でも経験豊富な開発者でも、しっかり理解していることが重要です C# 演算子 効率的で表現力豊かなコードを書くためには基本的です。 この包括的なガイドでは、C#のさまざまな種類の演算子と、それらをプログラムでどのように使用できるかを探ります。 これらのC#演算子を使用する方法も見てみましょう IronPDF.

C#の演算子の種類

算術演算子

C#における算術演算子は基本的な数学の操作に使用されます。 これには次のものが含まれます (+)減算 (-)乗算 (申し訳ありませんが、翻訳を行うための具体的なコンテンツが提供されていません。翻訳したい英語のテキストを入力してください。), 関数 (/)と剰余 (「%」). 算術演算子の場合、演算子の優先順位は、数学の演算子の優先順位としてよく知られているBEDMASやPEDMASと同様です。

これらの演算子がどのように機能するかを理解するために、例を詳しく見てみましょう。

// Arithmetic Operators
        int a = 10;
        int b = 3;
        int sum = a + b;
        int difference = a - b;
        int product = a * b;
        int quotient = a / b;
        int remainder = a % b;
        Console.WriteLine($"Arithmetic Operators:");
        Console.WriteLine($"Sum: {sum}, Difference: {difference}, Product: {product}, Quotient: {quotient}, Remainder: {remainder}");
        Console.WriteLine();
// Arithmetic Operators
        int a = 10;
        int b = 3;
        int sum = a + b;
        int difference = a - b;
        int product = a * b;
        int quotient = a / b;
        int remainder = a % b;
        Console.WriteLine($"Arithmetic Operators:");
        Console.WriteLine($"Sum: {sum}, Difference: {difference}, Product: {product}, Quotient: {quotient}, Remainder: {remainder}");
        Console.WriteLine();
' Arithmetic Operators
		Dim a As Integer = 10
		Dim b As Integer = 3
		Dim sum As Integer = a + b
		Dim difference As Integer = a - b
		Dim product As Integer = a * b
		Dim quotient As Integer = a \ b
		Dim remainder As Integer = a Mod b
		Console.WriteLine($"Arithmetic Operators:")
		Console.WriteLine($"Sum: {sum}, Difference: {difference}, Product: {product}, Quotient: {quotient}, Remainder: {remainder}")
		Console.WriteLine()
VB   C#

1.2. 関係演算子

関係演算子は値を比較し、それらの関係を判断するために使用されます。 C#で一般的な関係演算子には、より大きい (>)未満 (申し訳ありませんが、翻訳するコンテンツのテキストを提供してください。その後、英語から日本語に翻訳いたします。)「に等しい」 (==)等しくない (!=)以上 (以上)以下に日本語へ翻訳します

「以下および以下に等しい」 (<=).

// Relational Operators
        bool isEqual = (a == b);
        bool notEqual = (a != b);
        bool greaterThan = (a > b); // true if the left operand is greater than the right
        bool lessThan = (a < b); // true if the left operand is less than the right
        bool greaterOrEqual = (a >= b);
        bool lessOrEqual = (a <= 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();
// Relational Operators
        bool isEqual = (a == b);
        bool notEqual = (a != b);
        bool greaterThan = (a > b); // true if the left operand is greater than the right
        bool lessThan = (a < b); // true if the left operand is less than the right
        bool greaterOrEqual = (a >= b);
        bool lessOrEqual = (a <= 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();
' Relational Operators
		Dim isEqual As Boolean = (a = b)
		Dim notEqual As Boolean = (a <> b)
		Dim greaterThan As Boolean = (a > b) ' true if the left operand is greater than the right
		Dim lessThan As Boolean = (a < b) ' true if the left operand is less than the right
		Dim greaterOrEqual As Boolean = (a >= b)
		Dim lessOrEqual As Boolean = (a <= 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()
VB   C#

1.3. 論理演算子

論理演算子は、ブール値に対して論理演算を行うために使用されます。 C#の一般的な論理演算にはANDがあります。 (&& )または (**

)、NOT (!**). ANDとORは2つのオペランドを持つバイナリ演算子ですが、NOTは一元演算子の一つであり、1つのオペランドのみに影響を与えます。

// Logical Operators
        bool condition1 = true;
        bool condition2 = false;
        bool resultAnd = condition1 && condition2;
        bool resultOr = condition1 
 condition2;
        bool resultNot = !condition1;
        Console.WriteLine($"Logical Operators:");
        Console.WriteLine($"AND: {resultAnd}, OR: {resultOr}, NOT: {resultNot}");
        Console.WriteLine();
// Logical Operators
        bool condition1 = true;
        bool condition2 = false;
        bool resultAnd = condition1 && condition2;
        bool resultOr = condition1 
 condition2;
        bool resultNot = !condition1;
        Console.WriteLine($"Logical Operators:");
        Console.WriteLine($"AND: {resultAnd}, OR: {resultOr}, NOT: {resultNot}");
        Console.WriteLine();
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

1.4. 代入演算子

代入演算子は、変数に値を割り当てるために使用されます。 単純な代入演算子は = です。 ただし、C#には+=-=、*=/=%= などの複合代入演算子も用意されています。

// Assignment Operators
        int x = 5;
        int y = 2;
        x += y; // Equivalent to x = x + y
        y *= 3; // Equivalent to y = y * 3
        Console.WriteLine($"Assignment Operators:");
        Console.WriteLine($"x after +=: {x}, y after *=: {y}");
        Console.WriteLine();
// Assignment Operators
        int x = 5;
        int y = 2;
        x += y; // Equivalent to x = x + y
        y *= 3; // Equivalent to y = y * 3
        Console.WriteLine($"Assignment Operators:");
        Console.WriteLine($"x after +=: {x}, y after *=: {y}");
        Console.WriteLine();
' Assignment Operators
		Dim x As Integer = 5
		Dim y As Integer = 2
		x += y ' Equivalent to x = x + y
		y *= 3 ' Equivalent to y = y * 3
		Console.WriteLine($"Assignment Operators:")
		Console.WriteLine($"x after +=: {x}, y after *=: {y}")
		Console.WriteLine()
VB   C#

ビット単位演算子

ビット演算子はビットレベルで操作を行うバイナリ演算子です。 それらにはビット演算子が含まれています:ビット単位のAND ()ビット毎の OR ( )ビット単位の排他的論理和 (XOR) (^)ビット単位のNOT またはビット単位の補数( データが提供されていません。翻訳する内容を入力してください。)左シフト (入力されたテキストが見当たらないようです。翻訳を希望する具体的な内容を提供していただけますか?その後、専門用語と文脈の整合性を保つ翻訳を提供します。)、「右シフト」 (>>).

// Bitwise Operators
        int p = 5; // Binary: 0101
        int q = 3; // Binary: 0011
        int bitwiseAnd = p & q;
        int bitwiseOr = p 
 q;
        int bitwiseXor = p ^ q;
        int bitwiseNotP = ~p;
        int leftShift = p << 1;
        int rightShift = p >> 1;
        Console.WriteLine($"Bitwise Operators:");
        Console.WriteLine($"AND: {bitwiseAnd}, OR: {bitwiseOr}, XOR: {bitwiseXor}, NOT: {bitwiseNotP}, Left Shift: {leftShift}, Right Shift: {rightShift}");
        Console.WriteLine();
// Bitwise Operators
        int p = 5; // Binary: 0101
        int q = 3; // Binary: 0011
        int bitwiseAnd = p & q;
        int bitwiseOr = p 
 q;
        int bitwiseXor = p ^ q;
        int bitwiseNotP = ~p;
        int leftShift = p << 1;
        int rightShift = p >> 1;
        Console.WriteLine($"Bitwise Operators:");
        Console.WriteLine($"AND: {bitwiseAnd}, OR: {bitwiseOr}, XOR: {bitwiseXor}, NOT: {bitwiseNotP}, Left Shift: {leftShift}, Right Shift: {rightShift}");
        Console.WriteLine();
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

1.6. 条件演算子(三項演算子)

条件演算子 (お手伝いいたしますが、翻訳するテキストを提供してください。具体的な内容をお知らせいただければ、迅速に対応させていただきます。 申し訳ありませんが、具体的なテキストを提供してください。翻訳するコンテンツをお送りいただければ、喜んで日本語に翻訳いたします。) は、単一行でif-elseステートメントを表現するための短縮方法です。

// Conditional (Ternary) Operator
    int age = 20;
    string result = (age >= 18) ? "Adult" : "Minor";
    Console.WriteLine($"Conditional Operator:");
    Console.WriteLine($"Result: {result}");
    Console.WriteLine();
// Conditional (Ternary) Operator
    int age = 20;
    string result = (age >= 18) ? "Adult" : "Minor";
    Console.WriteLine($"Conditional Operator:");
    Console.WriteLine($"Result: {result}");
    Console.WriteLine();
' Conditional (Ternary) Operator
	Dim age As Integer = 20
	Dim result As String = If(age >= 18, "Adult", "Minor")
	Console.WriteLine($"Conditional Operator:")
	Console.WriteLine($"Result: {result}")
	Console.WriteLine()
VB   C#

この例では、age が 18 以上であれば result の値は "Adult" となり、それ以外の場合は "Minor" になります。

1.7. Null合体演算子

ヌル合体演算子 (*) は、null 許容型にデフォルト値を提供するために使用されます。

// Null-Coalescing Operator
        int? nullableValue = null;
        int resultCoalesce = nullableValue ?? 10;
        Console.WriteLine($"Null-Coalescing Operator:");
        Console.WriteLine($"Result: {resultCoalesce}");
// Null-Coalescing Operator
        int? nullableValue = null;
        int resultCoalesce = nullableValue ?? 10;
        Console.WriteLine($"Null-Coalescing Operator:");
        Console.WriteLine($"Result: {resultCoalesce}");
' Null-Coalescing Operator
		Dim nullableValue? As Integer = Nothing
		Dim resultCoalesce As Integer = If(nullableValue, 10)
		Console.WriteLine($"Null-Coalescing Operator:")
		Console.WriteLine($"Result: {resultCoalesce}")
VB   C#

1.8. すべてのC#オペレーターコード例の出力スクリーンショット

C# 演算子(開発者向けの動作方法):図1 - すべての演算子出力。

2. IronPDFの紹介

IronPDF C# 向けのIronPDFは、開発者がPDF関連機能を .NET アプリケーションにシームレスに統合することを可能にする多用途なライブラリです。 総合的なツールセットを提供するIronPDFは、PDFドキュメントの作成、修正、および情報の抽出を容易にします。 HTMLから動的PDFを生成する場合でも、ウェブサイトからコンテンツをキャプチャする場合でも、または高度なフォーマットを実行する場合でも、IronPDFは直感的なAPIを使用してこれらのプロセスを効率化します。

IronPDFは、レポート作成やドキュメント管理システムなど、PDF操作を必要とするアプリケーションで広く使用されています。IronPDFは複雑な作業を簡素化し、C#や.NET技術を駆使する開発者にとって貴重なリソースとなっています。 正確な使用方法や最新情報については、必ず公式ドキュメントを参照してください。

2.1. IronPDFの始め方

IronPDFをC#プロジェクトで使用し始めるには、最初にインストールが必要です IronPDF NuGetパッケージ. 以下のコマンドを使用してパッケージ マネージャー コンソールでこれを行うことができます:

Install-Package IronPdf

または、NuGet パッケージ マネージャーを使用して「IronPDF」を検索し、そこからパッケージをインストールすることもできます。

パッケージをインストールしたら、IronPDFを使用してPDFファイルをシームレスに処理することができます。

2.2. コード例:C# オペレーターのIronPDFの使用

using IronPdf;
using System;
class Program
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();
        // Add 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!");
    }
}
using IronPdf;
using System;
class Program
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();
        // Add 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!");
    }
}
Imports IronPdf
Imports System
Friend Class Program
	Shared Sub Main()
		Dim renderer = New ChromePdfRenderer()
		' Add content with mathematical operations
		Dim content As String = $"<!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 Mod 4}</p>
                            </body>
                            </html>"
		' Render HTML content to PDF
		Dim pdf = renderer.RenderHtmlAsPdf(content)
		' Save the PDF to a file
		pdf.SaveAs("MathOperations.pdf")
		Console.WriteLine("PDF with mathematical operations created successfully!")
	End Sub
End Class
VB   C#

このC#コードは、IronPDFライブラリを使用して、複数の演算子を含むPDFドキュメントを作成します。 それは、ChromePdfRenderer クラスを使用して HTML コンテンツをレンダリングし、C# 演算子を使用して計算された数式を含みます。

HTMLコンテンツには、合計、積、除算、および剰余のような結果を表示するタイトルや段落が含まれており、文字列フォーマットを使用して補間されています。 レンダリングされたHTMLはIronPDFを使用してPDFに変換され、その結果として生成されたPDFは「MathOperations.pdf」として保存されます。

C#オペレーター(開発者にはどう機能するか): 図2 - 前のコードから出力されたPDFドキュメント

結論

C#の演算子を習得することは、開発者にとって基本的なことであり、算術、関係、論理、代入、ビット単位、条件、およびnull合体演算を通じて効率的なコーディングを可能にします。 この包括的なガイドでは、さまざまな演算子タイプを実際のコード例と共に詳しく紹介しました。 さらに、IronPDFの導入により、C#でのExcel関連のタスクにおけるその有用性が強調されました。

C# 演算子をシームレスに統合することにより IronPDF開発者はこのライブラリの多様性を示しながら、PDFファイルでの算術操作を簡単に実行できます。 全体として、C# 演算子の深い理解は、開発者が幅広いプログラミングタスクのためにより堅牢で表現力豊かなコードを作成するのに役立ちます。

こちらのリンクを訪問して、IronPDFの無料トライアルライセンスを取得できます。 リンク. IronPDFについて詳しく知るには、訪問してください [以下の内容を日本語に翻訳します:

ここに

ご希望のイディオムや技術用語が追加されることによって、より適切な翻訳が提供できる場合もありますので、詳細なコンテキストを教えていただけると幸いです。](/docs/)およびコードの例については、以下のリンクをご覧ください [以下の内容を日本語に翻訳します:

ここに

ご希望のイディオムや技術用語が追加されることによって、より適切な翻訳が提供できる場合もありますので、詳細なコンテキストを教えていただけると幸いです。](/examples/using-html-to-create-a-pdf/).

< 以前
C# OAuth2(開発者向けの動作説明)
次へ >
C# nameof(開発者向けの動作方法)

準備はできましたか? バージョン: 2024.9 新発売

無料のNuGetダウンロード 総ダウンロード数: 10,659,073 View Licenses >