C#樹林– category –
プログラミング言語のC#を勉強したときのノートです。
-
C#樹林
[C#] Accelerating LINQ Queries with PLINQ (AsParallel)
C#'s LINQ (Language Integrated Query) is incredibly useful, but standard queries on IEnumerable<T> run sequentially on a single thread. When dealing with large data aggregation or CPU-intensive calculations, implementing PLINQ (Par... -
C#樹林
【C#】PLINQ (AsParallel) を使用してLINQクエリを並列化し高速処理する
C#のLINQ(Language Integrated Query)は非常に便利ですが、標準の IEnumerable<T> に対するクエリはシングルスレッドでシーケンシャル(順次)に実行されます。 大量のデータ集計やCPU負荷の高い計算を行う場合、PLINQ (Parallel LINQ) を導入する... -
C#樹林
[C#] Accelerating Data Processing with the Parallel Class
When processing large data collections, a standard foreach loop (sequential processing) uses only one CPU core. This can limit performance and lead to bottlenecks. The Parallel class, part of the C# Task Parallel Library (TPL), allows yo... -
C#樹林
【C#】Parallelクラスを用いたデータ並列処理の高速化実装
大量のデータコレクションに対して同一の処理を行う場合、foreach ループによる順次処理(シーケンシャル処理)ではCPUの単一コアしか利用できず、パフォーマンスが頭打ちになることがあります。 C#のタスク並列ライブラリ(TPL)に含まれる Parallel クラ... -
C#樹林
[C#] Safe Task Cancellation Using CancellationToken
To stop long-running asynchronous tasks (due to user actions or timeouts), the standard method is to use CancellationTokenSource and CancellationToken. This mechanism allows the caller to send a "stop command." The task detects this comm... -
C#樹林
【C#】CancellationTokenを使用したタスクの安全なキャンセル処理
長時間実行される非同期処理(Task)を、ユーザー操作やタイムアウト等の要因で途中停止(キャンセル)させるには、CancellationTokenSource と CancellationToken を使用するパターンが標準的です。 この仕組みを使うことで、呼び出し元から「停止命令」... -
C#樹林
[C#] Correct Usage of Task.Delay for Non-Blocking Waits
When pausing execution for a certain period in C#, using the traditional Thread.Sleep completely stops (blocks) the running thread. This causes UI freezes or thread pool exhaustion in web servers. In modern .NET development, the standard... -
C#樹林
【C#】スレッドをブロックしない待機処理 Task.Delay の正しい使い方
C#で一定時間処理を停止させる際、古くからある Thread.Sleep を使用すると、実行中のスレッドそのものを完全に停止(ブロック)させてしまいます。これはUIのフリーズや、Webサーバーにおけるスレッド枯渇の原因となります。 現代の.NET開発では、async/a... -
C#樹林
【C#】Taskの実行を指定時間だけ待機し、タイムアウト判定を行う方法
非同期処理(Task)を実行する際、処理が無限にブロックすることを防ぐために「最大N秒まで待つ」というタイムアウト処理を実装するのは一般的な要件です。 C#の Task クラスには、処理の完了を同期的に待機する Wait メソッドが存在し、引数に制限時間を... -
C#樹林
[C#] Offloading High-CPU Synchronous Tasks with Task.Run
Executing CPU-intensive tasks (CPU-bound operations), such as image processing or complex calculations, on the main thread can cause the application's UI to freeze. In this article, I will explain an implementation pattern where such syn... -
C#樹林
【C#】CPU負荷の高い同期処理をTask.Runで非同期メソッド化する方法
画像処理や複雑な計算など、CPUリソースを大量に消費する処理(CPUバウンドな処理)をメインスレッドで実行すると、アプリケーションのUIがフリーズする原因となります。 こうした同期的な計算処理を Task.Run を用いてバックグラウンドスレッドにオフロー... -
C#樹林
[C#] Basic Implementation of Asynchronous Web Access using async/await and HttpClient
Asynchronous programming in C# allows you to write highly readable code that looks similar to synchronous code by using the async modifier and the await operator. This technology is essential for maintaining application responsiveness an... -
C#樹林
【C#】async/awaitとHttpClientを用いた非同期Webアクセスの基礎実装
C#における非同期プログラミングは、async 修飾子と await 演算子を使用することで、同期処理とほぼ変わらない可読性の高いコードで記述できます。特にネットワーク通信のようなI/O待ちが発生する処理において、アプリケーションの応答性を維持しつつシス... -
C#樹林
[C#] Implementing Background Processing with BackgroundWorker (Progress Reporting & Cancellation)
The BackgroundWorker class, available since .NET Framework 2.0, is a component for implementing event-based asynchronous processing. While the Task class and async/await syntax are mainstream today, BackgroundWorker is still used for mai... -
C#樹林
【C#】BackgroundWorkerを使用したバックグラウンド処理の実装(進捗報告・キャンセル対応)
.NET Framework 2.0から存在する BackgroundWorker クラスは、イベントベースで非同期処理を実装するためのコンポーネントです。現在では Task クラスや async/await 構文が主流ですが、Windows Formsなどの既存アプリケーションの保守や、イベント駆動型... -
C#樹林
[C#] Implementing Dynamic Type Resolution and Duck Typing with the dynamic Keyword
By using the dynamic keyword introduced in C# 4.0, you can implement dynamic behavior where type resolution happens at runtime, even though C# is a statically typed language. Usually, C# checks for the existence of methods and properties... -
C#樹林
【C#】dynamic型を活用した動的な型解決とダックタイピングの実装
C# 4.0で導入された dynamic キーワードを使用すると、静的型付け言語であるC#において、実行時に型解決を行う動的な振る舞いを実装できます。 通常、C#ではコンパイル時にメソッドやプロパティの存在確認が行われますが、dynamic型を使用することでこのチ... -
C#樹林
[C#] Simplifying Array Operations with Index and Range Structs
With the introduction of Index (the ^ operator) and Range (the .. operator) in C# 8.0, accessing elements and slicing collections (arrays, lists, spans) has become much more intuitive and safe. By utilizing these features, you can elimin... -
C#樹林
【C#】配列操作を劇的に簡潔にするIndex構造体とRange構造体の活用
C# 8.0以降で導入された Index(^演算子)と Range(..演算子)を活用することで、配列やコレクションの要素アクセス、およびスライス操作(範囲抽出)が非常に直感的かつ安全に記述できるようになりました。 従来の Length - 1 のような冗長な記述を排除... -
C#樹林
Returning Multiple Values from Methods Using Tuples (ValueTuple) in C#
Using Tuples (ValueTuple), available since C# 7.0, allows you to easily return multiple values from a method. Previously, returning multiple values required using out arguments or defining specific classes or structures for the return va... -
C#樹林
【C#】タプル(ValueTuple)を使用してメソッドから複数の値を返す
C# 7.0 以降で利用可能な「タプル(ValueTuple)」を使用すると、メソッドから複数の値を簡単に返すことができます。 従来、複数の値を返すためには out 引数を使用するか、戻り値専用のクラスや構造体を定義する必要がありました。タプルを利用することで... -
C#樹林
[C#] Defining Immutable Objects Using Init-Only Setters
Introduced in C# 9.0, init accessors (init-only setters) allow you to easily define properties that can only be set during object initialization and become immutable (read-only) afterwards. Previously, creating immutable objects required... -
C#樹林
【C#】init専用セッターを使用して不変(Immutable)なオブジェクトを定義する
C# 9.0 で導入された init アクセサー(init-only setters)を使用すると、**オブジェクトの初期化時のみ値を設定可能で、その後は変更不可能(読み取り専用)**なプロパティを簡単に定義できます。 従来、不変なオブジェクトを作成するには「読み取り専用... -
C#樹林
[C#] Concisely Defining Property Get/Set Accessors Using Expression Bodies (=>)
Since C# 7.0, you can write both property get and set accessors as "expression-bodied members." This allows you to simply write property definitions using the arrow operator =>, replacing the traditional block { ... } and return state... -
C#樹林
【C#】プロパティのget/setアクセサーを式形式(=>)で簡潔に定義する
C# 7.0 から、プロパティの get アクセサーと set アクセサーの両方を「式形式のメンバー(Expression-bodied members)」として記述できるようになりました。 従来はブロック { ... } と return 文を用いて記述していたプロパティ定義を、アロー演算子 =&... -
C#樹林
[C#] Hiding Helper Methods within Scope Using Local Functions
Introduced in C# 7.0, Local Functions allow you to define a function directly inside another method. Previously, even small helper methods used only by a specific method had to be defined as private methods of the class. This often clutt... -
C#樹林
【C#】ローカル関数(Local Functions)でヘルパーメソッドをスコープ内に隠蔽する
C# 7.0 で導入された「ローカル関数」を使用すると、メソッドの内部に、そのメソッド専用の関数を定義することができます。 従来、特定のメソッドからしか呼ばれない小さなヘルパーメソッドであっても、クラスの private メソッドとして定義する必要があり... -
C#樹林
Simplifying Conditional Expressions with C# is Operator Pattern Matching (and/or/not)
By using the enhanced pattern matching introduced in C# 9.0, you can use the is operator to write value range checks, null checks, and logical operations (AND, OR) in a very intuitive and concise way. It allows you to reduce repetitive v... -
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...