C#樹林– category –
プログラミング言語のC#を勉強したときのノートです。
-
C#樹林
【C#】IEquatable
を実装して自作クラスの等価判定を定義する C#において、自作したクラス(参照型)をコレクション(ListやHashSetなど)で扱う際、意図した通りに「等しい」と判定されないことがあります。これは、クラスのデフォルトの比較動作が「値(プロパティ)の一致」ではなく「参照(メモリアドレス)の一致... -
C#樹林
[C#] Implementing Iterator Methods Returning IEnumerable Using yield return
In C#, when implementing a method that returns a collection or sequence (a series of data), it is often recommended to use the yield return syntax instead of creating and returning a collection like List<T>. Using yield allows the ... -
C#樹林
【C#】yield returnを使ってIEnumerable
を返すイテレータメソッドを実装する C#において、コレクションやシーケンス(データの並び)を返すメソッドを実装する場合、List<T>などのコレクションを生成して返す代わりに、yield return構文を使用することが推奨されるケースが多くあります。 yieldを使用すると、コンパイラが自動... -
C#樹林
【C#】インターフェイスの既定の実装を上書きし、元のロジックを再利用するテクニック
C# 8.0で導入された「インターフェイスの既定の実装(Default Interface Methods)」は、インターフェイス自体にロジックを持たせることを可能にしました。 通常、実装クラス側で同じメソッドを定義すると、インターフェイスの既定実装は完全に無視(上書... -
C#樹林
[C#] Defining Default Behavior (Default Implementation) for Methods in Interfaces
Before C# 8.0, interfaces were strictly for defining "contracts" and could not contain concrete processing (implementation). However, with the language specification extensions in C# 8.0, it became possible to define "Default Interface M... -
C#樹林
【C#】インターフェイスにメソッドの既定動作(デフォルト実装)を定義する
C# 8.0以前では、インターフェイスはあくまで「契約」を定義するものであり、具体的な処理(実装)を持つことはできませんでした。しかし、C# 8.0での言語仕様の拡張により、インターフェイスのメソッドに「既定の実装(Default Interface Methods)」を持... -
C#樹林
[C#] Basics of Class Design: Defining and Implementing Interfaces
In Object-Oriented Programming like C#, "Interfaces" play a vital role. An interface defines the "functional standard (contract)" that a class must implement. Using interfaces allows for the separation of specific processing details (imp... -
C#樹林
【C#】インターフェイスの定義と実装によるクラス設計の基礎
C#などのオブジェクト指向プログラミングにおいて、「インターフェイス(Interface)」は非常に重要な役割を果たします。インターフェイスとは、クラスが実装すべき「機能の規格(契約)」を定義したものです。 インターフェイスを利用することで、具体的... -
C#樹林
【C#】ジェネリックメソッドの定義と型推論によるコードの共通化
C#において、異なるデータ型に対して「同じロジック」を適用したい場合、ジェネリックメソッド(Generic Method)を定義するのが最も効率的です。 int型、double型、DateTime型など、それぞれの型専用にオーバーロードメソッドを作成するのはコードの重複... -
C#樹林
[C#] Defining Generic Classes and Universal Range Checking with IComparable
Generics in C# allow for the definition of versatile classes and methods that do not depend on specific types. By utilizing this feature, code duplication can be eliminated, and highly reusable logic can be built while maintaining type s... -
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...