Channel.Create
Learn to create channels with Channel.CreateBounded and CreateUnbounded.
Channel.Create
Channel<T> builds an asynchronous queue between a producer and a consumer. Channel.CreateUnbounded<T>() makes an unbounded channel, Channel.CreateBounded<T>(capacity) a fixed-capacity one.
A channel has two sides: the Writer for the producer and the Reader for the consumer. This split keeps pipeline architecture clean. Channels live under System.Threading.Channels.
Bounded channel and capacity
A bounded channel blocks writes in a controlled way once the capacity is full. Channel.CreateBounded<int>(5) opens a five-item buffer. A sixth write waits until an item is consumed.
This gives backpressure that balances a fast producer against a slow consumer. The channel's Writer and Reader properties provide this control.
Exercise Create both channel kinds
Create an unbounded string channel named unbounded and a bounded int channel with capacity 5 named bounded. Print a confirmation line for each.
Show example solution
var unbounded = Channel.CreateUnbounded<string>(); var bounded = Channel.CreateBounded<int>(5); Console.WriteLine("unbounded created"); Console.WriteLine("bounded capacity 5");
Example solution:
using System;
using System.Threading.Channels;
class Program
{
static void Main()
{
var unbounded = Channel.CreateUnbounded<string>();
var bounded = Channel.CreateBounded<int>(5);
Console.WriteLine("unbounded created");
Console.WriteLine("bounded capacity 5");
}
}
Test yourself
-
Which method creates an unbounded channel?
- Channel.CreateUnbounded<T>()
- Channel.New<T>()
- Channel.Open<T>()
- Channel.Make<T>()
Show answer
Channel.CreateUnbounded<T>() — Correct. CreateUnbounded makes an unbounded channel.
-
Complete the CreateUnbounded call
var channel = Channel.____;Show answer
var channel = Channel.CreateUnbounded<string>(); -
Match each channel factory
- CreateUnbounded<T>()
- Unbounded channel
- CreateBounded<T>(n)
- Bounded channel (n items)
- Channel<T>
- Base channel type