C#樹林– category –
プログラミング言語のC#を勉強したときのノートです。
-
C#樹林
【C#】is演算子のパターンマッチング(and/or/not)で条件式を簡潔にする
C# 9.0 で強化されたパターンマッチングを利用すると、is 演算子を用いて、値の範囲チェックや null チェック、論理演算(かつ、または)を非常に直感的かつ簡潔に記述できるようになりました。 従来、&& や || を用いて繰り返していた変数記述を... -
C#樹林
[C#] Conditional Logic in Switch Case Labels (When Clause and Pattern Matching)
Since C# 7.0, the functionality of the switch statement has been significantly expanded to support Pattern Matching. This allows for not only simple constant matching but also type checking and detailed conditional logic using the when c... -
C#樹林
【C#】switch文のcaseラベルで条件判定を行う(when句とパターンマッチング)
C# 7.0 以降、switch 文の機能が大幅に拡張され、「パターンマッチング」が利用可能になりました。これにより、単なる定数の一致判定だけでなく、型の判定や、when 句を用いた詳細な条件判定が可能になっています。 従来は if-else 文を連ねて記述していた... -
C#樹林
[C#] Writing Concise Conditional Logic with Switch Expressions
Introduced in C# 8.0, Switch Expressions allow for more concise and intuitive coding compared to traditional switch statements. While traditional switch statements were "control structures (statements)," switch expressions are "expressio... -
C#樹林
【C#】switch式を使って条件分岐を簡潔に記述する
C# 8.0 で導入された「switch式(Switch Expressions)」を使用すると、従来の switch 文よりも簡潔で直感的な記述が可能になります。 従来の switch 文は「制御構造(ステートメント)」でしたが、switch式は「式(Expression)」であるため、結果を直接... -
C#樹林
[C#] Using Named Arguments to Improve Readability and Flexibility of Method Calls
Introduced in C# 4.0, Named Arguments allow you to explicitly specify the name of an argument when calling a method. Normally, method arguments must be passed in the order they are defined. However, by using named arguments, you can free... -
C#樹林
【C#】名前付き引数を利用してメソッド呼び出しの可読性と柔軟性を向上させる
C# 4.0 で導入された「名前付き引数(Named Arguments)」を使用すると、メソッドを呼び出す際に引数の名前を明示的に指定して値を渡すことができます。 通常、メソッドの引数は定義された順序通りに渡す必要がありますが、名前付き引数を使用することで、... -
C#樹林
[C#] Cleaning Up Code by Inlining Method Argument Definitions with Out Variable Declarations
Introduced in C# 7.0, Out variables allow you to declare a variable directly within the argument list when calling a method that has an out parameter. This eliminates the need to write a separate line to declare the variable beforehand, ... -
C#樹林
【C#】out変数宣言でメソッドの引数定義をインライン化し、コードをスッキリさせる
C# 7.0 で導入された「out 変数宣言(Out variables)」を使用すると、out 引数を持つメソッドを呼び出す際に、その引数部分で直接変数を宣言できるようになりました。 これにより、以前のように変数を事前に宣言するための行を記述する必要がなくなり、コ... -
C#樹林
[C#] Omitting Type Names in Instance Creation with Target-Typed New Expressions
Introduced in C# 9.0, Target-typed new expressions allow you to omit the type description when creating an instance and simply write new() provided the compiler can infer the type from the context. Previously, code tended to become verbo... -
C#樹林
【C#】ターゲット型new式を使ってインスタンス生成の型名を省略する
C# 9.0 で導入された「ターゲット型 new 式(Target-typed new expressions)」を使用すると、コンパイラが型を推論できる文脈において、インスタンス生成時の型記述を省略し、単に new() と記述することができます。 従来、型名が長いクラスやジェネリッ... -
C#樹林
[C#] Concisely Setting Default Values for Method Arguments Using the default Literal
Introduced in C# 7.1, the default literal allows you to omit the type name when specifying the default value of a type. This allows you to replace the verbose default(T) expression with a simple default when defining optional arguments f... -
C#樹林
【C#】defaultリテラルを使用してメソッド引数の既定値を簡潔に設定する
C# 7.1 で導入された default リテラルを使用すると、型の既定値(デフォルト値)を記述する際に型名を省略できるようになりました。 これにより、メソッドのオプション引数(省略可能な引数)を定義する際、default(T) と記述していた冗長な表現を defaul... -
C#樹林
[C#] Writing Safe Code by Eliminating Hardcoded Strings with the nameof Operator
The nameof operator, introduced in C# 6.0, is a feature that allows you to obtain the name of a type, member, or variable as a string at compile time. Previously, names were written as string literals (e.g., "variableName") when outputti... -
C#樹林
【C#】nameof演算子を使って文字列のハードコーディングを排除し、安全なコードを書く
C# 6.0 で導入された nameof 演算子は、型、メンバー、変数などの名前をコンパイル時に文字列として取得する機能です。 従来、ログ出力や例外のスロー時に対象の名前を文字列リテラル("variableName")として記述していましたが、これには「変数名を変更... -
C#樹林
[C#] Reducing Code Verbosity by Omitting Class Names with the using static Directive
The using static directive, introduced in C# 6.0, allows you to call static members (methods and properties) of a specified type directly without writing the type name every time. By applying this feature to frequently used static classe... -
C#樹林
【C#】using staticディレクティブでクラス名を省略し、コード記述量を減らす
C# 6.0 で導入された using static ディレクティブを使用すると、指定した型の静的メンバー(メソッドやプロパティ)を、型名を省略して直接呼び出すことができます。 頻繁に使用する静的クラス(System.Console や System.Math など)に対してこの機能を... -
C#樹林
[C#] Simplifying Code with Top-Level Statements (Omitting the Main Method)
Introduced in C# 9.0, Top-level statements allow you to omit the explicit entry point (Main method) that was traditionally required. This enables you to start programs simply, much like scripting languages, and significantly reduces boil... -
C#樹林
【C#】最上位レベルステートメントでMainメソッドを省略してコードを簡潔にする
C# 9.0 から導入された「最上位レベルステートメント(Top-level statements)」を使用すると、従来のエントリーポイント(Main メソッド)を明示的に記述する必要がなくなります。これにより、スクリプト言語のようにシンプルにプログラムを開始でき、特... -
C#樹林
Implementing Event Notifications with IObserver and IObservable in C#
In .NET Framework and .NET Core, System.IObservable<T> and System.IObserver<T> are the standard interfaces provided for implementing the Observer Pattern. By using these interfaces, you can build a push-based notification sys... -
C#樹林
【C#】IObserverとIObservableを用いたイベント通知の実装
.NET Frameworkおよび.NET Core以降では、オブザーバーパターン(Observer Pattern)を実装するための標準インターフェースとして、System.IObservable<T> と System.IObserver<T> が用意されています。これらを利用することで、データ発行元... -
C#樹林
[C#] Implementing the IDisposable Interface to Properly Release Resources
While the .NET Garbage Collector (GC) automates memory management, it does not manage "unmanaged resources" such as file handles, database connections, or network sockets. To properly release these resources, a class must implement the I... -
C#樹林
【C#】IDisposableインターフェイスを実装してリソースを適切に解放する
.NETのガベージコレクタ(GC)はメモリ管理を自動化しますが、ファイルハンドル、データベース接続、ネットワークソケットといった「アンマネージドリソース」までは管理しません。これらのリソースを適切に解放するためには、クラスにIDisposableインター... -
C#樹林
[C#] Sorting Collections with Custom Rules by Implementing IComparer
When using the List<T>.Sort() method, there are cases where you want to sort based on a specific property or a special calculation formula, rather than the default order (e.g., ascending order). Implementing IComparable<T> on... -
C#樹林
【C#】IComparer
を実装して独自のルールでコレクションをソートする List<T>.Sort()メソッドを使用する際、デフォルトの並び順(昇順など)ではなく、「特定のプロパティに基づいて並び替えたい」「特殊な計算式で順序を決めたい」という場合があります。 クラス自体にIComparable<T>を実装するとそのクラスの「... -
C#樹林
[C#] Implementing IComparable for Custom Class Comparison and Sorting
C#'s List<T>.Sort() and Array.Sort() methods work out-of-the-box for basic types like numbers and strings. However, if you call them on a custom class or struct, an exception (InvalidOperationException) occurs because the program d... -
C#樹林
【C#】IComparable
を実装して自作クラスの大小比較とソートを行う C#のList<T>.Sort()メソッドやArray.Sort()メソッドは、数値や文字列などの基本的な型であればそのまま機能しますが、自作のクラスや構造体に対して呼び出すと、どのように順序を付ければよいか分からず例外(InvalidOperationException)が発生しま... -
C#樹林
[C#] Implementing Equality Logic Outside Classes with IEqualityComparer
Usually, equality determination for a class (whether two objects are the same) is defined by implementing IEquatable<T> within the class itself. However, this approach may not be suitable in the following cases: When you want to ch... -
C#樹林
【C#】IEqualityComparer
でクラスの外部に等価判定ロジックを持たせる 通常、クラスの等価判定(2つのオブジェクトが同じかどうか)は、そのクラス自身にIEquatable<T>を実装して定義します。しかし、以下のようなケースではそれが適さない場合があります。 ソースコードを変更できないクラス(外部ライブラリなど)の比... -
C#樹林
[C#] Defining Equality for Custom Classes by Implementing IEquatable
In C#, when handling custom classes (reference types) in collections like List or HashSet, you may find that they are not determined to be "equal" as intended. This is because the default comparison behavior of classes is based on "refer...