What are channels in C# and how do they work?

Answer

Channels (System.Threading.Channels) are an asynchronous, thread-safe producer-consumer communication primitive — the .NET equivalent of Go's channels. Create: var channel = Channel.CreateUnbounded<int>(); or Channel.CreateBounded<int>(100) (backpressure when full). Writer: await channel.Writer.WriteAsync(item);. Reader: await foreach (var item in channel.Reader.ReadAllAsync(cancellationToken)) { Process(item); }. Signal completion: channel.Writer.Complete();. Use cases: (1) Producer-consumer pipelines. (2) Batching work items. (3) Rate-limited processing. (4) Fan-out (one writer, multiple readers). Advantages over BlockingCollection<T>: fully async (no thread blocking), better performance, supports IAsyncEnumerable. Compared to SignalR/queues: channels are in-process only. For multi-process pipelines, use a message broker (RabbitMQ, Azure Service Bus).