mori– Author –
-
C#樹林
【C#】ジェネリッククラスの定義とIComparableによる汎用的な範囲判定
C#におけるジェネリック(Generics)は、特定の型に依存しない汎用的なクラスやメソッドを定義するための機能です。これを活用することで、コードの重複を排除し、型安全性を維持したまま再利用性の高いロジックを構築できます。 今回は、数値や日付など「... -
C#樹林
[C#] Enhancing Robustness and Equality Checks with record Types
The record type, introduced in C# 9.0, is a reference type specialized for representing collections of data. When using traditional class types, ensuring data equality (treating objects as "equal" if all values are the same) and immutabi... -
C#樹林
【C#】record型を活用して堅牢性と等価判定を強化する
C# 9.0で導入されたrecord型は、データのまとまりを表現することに特化した参照型です。 従来のclassを使用する場合、データの等価性判定(値がすべて同じなら「等しい」とみなすこと)や、不変性(Immutability)を担保するためには、多くのボイラープレ... -
C#樹林
[C#] Implementing a Class to Add Properties at Runtime by Inheriting DynamicObject
Although C# is a statically typed language, you can create classes that dynamically add and retrieve properties at runtime by using the dynamic keyword and the System.Dynamic.DynamicObject class. This is an effective technique when handl... -
C#樹林
【C#】DynamicObjectを継承して実行時にプロパティを追加できるクラスを実装する
C#は静的型付け言語ですが、dynamicキーワードとSystem.Dynamic.DynamicObjectクラスを利用することで、実行時に動的にプロパティを追加・取得するクラスを作成できます。 これは、JSONのようなスキーマレスなデータを扱う場合や、コンパイル時には構造が... -
C#樹林
[C#] Intuitive Handling of Custom Classes with Operator Overloading
In C#, the behavior of standard operators (such as ==, +, <) can be independently defined for classes and structures. This is called "Operator Overloading." By implementing this appropriately, even custom types can be compared and cal... -
C#樹林
【C#】演算子のオーバーロードで自作クラスを直感的に扱う
C#では、クラスや構造体に対して標準の演算子(==、+、< など)の動作を独自に定義することができます。これを「演算子のオーバーロード」と呼びます。 適切に実装することで、自作の型であっても数値や文字列と同じように自然な記法で比較や計算が可能... -
C#樹林
[C#] Defining Extension Methods for LINQ to Objects
C# LINQ is very powerful, but standard methods alone may not meet specific project requirements. In such cases, defining extension methods for the IEnumerable<T> interface allows you to add custom processing that can be called with... -
C#樹林
【C#】LINQ to Objectsに対応した拡張メソッドを定義する
C#のLINQは非常に強力ですが、標準メソッドだけではプロジェクト固有の要件を満たせない場合があります。そのような場合、IEnumerable<T>インターフェースに対する拡張メソッドを定義することで、標準のLINQメソッド(WhereやSelectなど)と同じ感覚... -
C#樹林
[C#] Adding Functionality to Existing Classes by Defining Extension Methods
C# has a feature called "Extension Methods" that allows you to add new methods to existing types (classes, structs, interfaces) without using inheritance or directly modifying the source code. This allows you to implement utility functio... -
C#樹林
【C#】拡張メソッドを定義して既存クラスに機能を追加する
C#には、継承を使用したりソースコードを直接修正したりすることなく、既存の型(クラスや構造体、インターフェース)に新しいメソッドを追加できる「拡張メソッド」という機能があります。 これにより、.NET標準のクラス(stringやList<T>など)や... -
C#樹林
[C#] Defining Custom Events and Notifying Data Using EventHandler
"Events" in C# are an important mechanism for achieving loose coupling between classes. They are frequently used not only for GUI events like button clicks but also in business logic to notify external parts about "state changes" or "thr... -
C#樹林
【C#】自作イベントの定義とEventHandler
を利用したデータ通知 C#における「イベント」は、クラス間で疎結合な通知機能を実現するための重要な機構です。ボタンクリックなどのGUIイベントだけでなく、ビジネスロジックにおいても「状態の変化」や「閾値の超過」などを外部に通知する際に多用されます。 かつては独自の... -
C#樹林
Mastering C# Indexers: Treat Objects Like Arrays
In C#, you can provide functionality to access data within instances of classes or structs using brackets [], just like arrays. This feature is called an Indexer. Using indexers improves the intuitive operability of classes that hold col... -
C#樹林
【C#】クラスを配列のように扱うインデクサ(Indexer)の定義とオーバーロード
C#では、クラスや構造体のインスタンスに対して、配列と同じようにブラケット [] を使用してデータにアクセスする機能を提供できます。これを「インデクサ(Indexer)」と呼びます。 インデクサを使用することで、内部にコレクションを持つクラス(ラッパ... -
C#樹林
[C#] Defining Methods That Accept Lambda Expressions Using Action, Func, and Predicate
In C#, you can accept "processing logic itself" (such as lambda expressions or methods) as arguments for a method. This allows you to create flexible and reusable methods where the caller can decide a specific part of the behavior. While... -
C#樹林
【C#】Action, Func, Predicateを活用してラムダ式を受け取るメソッドを定義する
C#では、メソッドの引数として「処理そのもの(ラムダ式やメソッド)」を受け取ることができます。これにより、処理の一部を呼び出し元で決定できる、柔軟で再利用性の高いメソッドを作成可能です。 以前は独自のdelegate型を定義する必要がありましたが、... -
C#樹林
[C#] How to Reduce Nesting and Simplify Code with using Declarations
The "using declaration" introduced in C# 8.0 is a feature that drastically simplifies the syntax for resource management. With traditional using statements, the code readability often suffered due to deep nesting caused by { ... } blocks... -
C#樹林
【C#】using宣言でネストを削減しコードを簡潔にする方法
C# 8.0で導入された「using宣言(using declaration)」は、リソース管理に関する記述を劇的にシンプルにする機能です。 従来のusingステートメントでは、ブロック{ ... }によるネスト(階層)が深くなり、コードの可読性が下がるという課題がありました。... -
C#樹林
[C#] How to Reliably Release Resources with the using Statement and IDisposable
In programs that handle external resources (unmanaged resources) such as file operations or database connections, "cleanup" after use is extremely important. Forgetting to release (Dispose) resources can cause files to remain locked or l... -
C#樹林
【C#】usingステートメントでリソースを確実に解放する方法とIDisposable
ファイル操作やデータベース接続など、外部リソース(アンマネージドリソース)を扱うプログラムにおいて、使用後の「後始末」は極めて重要です。リソースを開放(Dispose)し忘れると、ファイルがロックされたままになったり、メモリリークを引き起こした... -
C#樹林
[C#] How to Ensure Cleanup Runs Even After a Return Statement Using try-finally
When a method is executing, there are often tasks you must perform regardless of how the method ends—whether an exception occurs, it finishes normally, or it exits early via a return statement. These tasks might include releasing resourc... -
C#樹林
【C#】try-finally文でreturn時にも確実に後処理を実行する方法
メソッドの実行中に例外が発生した場合だけでなく、正常に終了した場合や、途中でreturn文によってメソッドを抜ける場合であっても、必ず実行したい処理(リソースの解放、ログの出力、状態の復元など)が存在します。 C#のtry-finally構文を使用すること... -
C#樹林
[C#] Using Throw Expressions in Conditional and Null-Coalescing Operators
Before C# 7.0, throw was treated only as a "statement," meaning it had to be written inside blocks like if statements. However, since C# 7.0, throw can be written as an "expression." This allows you to throw exceptions directly in places... -
C#樹林
【C#】条件演算子やnull合体演算子の中で例外を投げるthrow式
C# 7.0以前では、throwは「文(Statement)」としてのみ扱われていたため、if文などのブロック内で記述する必要がありました。しかし、C# 7.0以降ではthrowを「式(Expression)」として記述できるようになったため、条件演算子(?:)やnull合体演算子(??... -
C#樹林
[C#] Catching Exceptions Under Specific Conditions Using Exception Filters (when clause)
Introduced in C# 6.0, Exception Filters allow you to specify detailed conditions for entering a catch block. This makes it possible to decide whether to catch an exception based not only on the exception type but also on the values of pr... -
C#樹林
【C#】例外フィルタ(when句)を使用して特定の条件下でのみ例外を捕捉する
C# 6.0で導入された「例外フィルタ(Exception Filters)」を使用すると、catchブロックに入る条件を詳細に指定できます。これにより、単に例外の型だけでなく、例外オブジェクトが持つプロパティの値や、外部の状態に基づいて、捕捉するかどうかを決定す... -
C#樹林
[C#] How to Properly Define and Implement Custom Exceptions
While C# provides many standard exception classes like ArgumentException and InvalidOperationException, relying solely on them can sometimes make the meaning of errors ambiguous when expressing application-specific business logic errors ... -
C#樹林
【C#】独自例外(カスタム例外)を適切に定義・実装する方法
C#にはArgumentExceptionやInvalidOperationExceptionなど、多くの標準例外クラスが用意されています。しかし、アプリケーション固有のビジネスロジックエラー(例:在庫不足、アカウントロック、残高不足など)を表現する場合、標準の例外クラスだけでは... -
C#樹林
[C#] Extension Method to Recursively Enumerate InnerExceptions and Identify the Root Cause
In large-scale application development, especially in systems adopting a multi-layered architecture (Web API, Business Logic, Data Access Layer, etc.), it is common for exceptions to be "wrapped" and propagated to upper layers. For examp...