C#樹林– category –
プログラミング言語のC#を勉強したときのノートです。
-
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... -
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#] Technique to Override Default Interface Implementations and Reuse Original Logic
The "Default Interface Methods" feature introduced in C# 8.0 made it possible for interfaces themselves to hold logic. Normally, if you define the same method in the implementing class, the default implementation in the interface is comp... -
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#] Code Sharing via Generic Method Definitions and Type Inference
In C#, when you want to apply the "same logic" to different data types, defining a Generic Method is the most efficient approach. Creating overload methods for every specific type, such as int, double, or DateTime, leads to code duplicat... -
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...