Skip to Content
Concurrency in C# Cookbook、第 2 版
book

Concurrency in C# Cookbook、第 2 版

by Stephen Cleary
May 2025
Intermediate to advanced
254 pages
3h 28m
Japanese
O'Reilly Media, Inc.
Content preview from Concurrency in C# Cookbook、第 2 版

第11章. 関数フレンドリーのOOP

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

最近のプログラムは非同期プログラミングを必要とする。最近のサーバはこれまで以上にスケーラブルでなければならないし、エンドユーザのアプリケーションはこれまで以上にレスポンスが良くなければならない。開発者は非同期プログラミングを学ばなければならないことを発見しているが、この世界を探求するうちに、慣れ親しんだ伝統的なオブジェクト指向プログラミングと衝突することが多いことに気づく。

その理由の核心は、非同期プログラミングが機能的だからである。関数型」というのは「関数として機能する」という意味ではなく、手続き型プログラミングではなく関数型プログラミングスタイルであるという意味だ。多くの開発者は大学で関数型プログラミングの基礎を学んだが、それ以来ほとんど触れていない。もし、(car (cdr '(3 5 7))) のようなコードを見て、抑圧された記憶がよみがえって寒気がするのなら、あなたはその部類に入るかもしれない。しかし恐れることはない。現代の非同期プログラミングは、慣れればそれほど難しくはない。

async 、大きなブレークスルーは、非同期プログラミングをしながらも手続き的に考えることができるということだ。これにより、非同期メソッドが書きやすく、理解しやすくなった。しかし、その裏側では、非同期コードは依然として機能的な性質を持っており、async メソッドを古典的なオブジェクト指向設計に無理やり組み込もうとすると、いくつかの問題が発生する。この章のレシピは、非同期コードがオブジェクト指向プログラミングと衝突する摩擦点に対処するものである。

これらの摩擦点は、既存のOOPコードベースをasync-フレンドリーなコードベースに変換するときに特に顕著になる。

11.1 非同期インタフェースと継承

問題

、非同期にしたいメソッドがインタフェースや基底クラスにある。

解決策

この問題とその解決策を理解する鍵は、async が実装の詳細であることを理解することである。async キーワードは実装を持つメソッドにしか適用できない。抽象メソッドやインタフェース・メソッドに適用することはできない(デフォルトの実装がある場合を除く)。しかし、async キーワードがないだけで、async メソッドと同じシグネチャを持つメソッドを定義することはできる。

型は メソッドではなく、待ち受け可能であることを忘れてはならない。メソッドが返すTask は、そのメソッドがasync を使って実装されているかどうかにかかわらず、await することができる。つまり、インタフェースや抽象化メソッドはTask (またはTask<T> )を返すことができ、そのメソッドの戻り値は待ち受け可能である。

次のコードは、非同期メソッドを持つインターフェイス(async キーワードなし)、そのインターフェイスの実装(async )、インターフェイスのメソッドを消費する独立したメソッド(await )を定義している:

interface IMyAsyncInterface
{
  Task<int> CountBytesAsync(HttpClient client, string url);
}

class MyAsyncClass : IMyAsyncInterface
{
  public async Task<int> CountBytesAsync ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Async in C# 5.0

Async in C# 5.0

Alex Davies

Publisher Resources

ISBN: 9798341650060