I made a Goroutine-inspired equivalent in C#
Hey everyone,
I've been a longtime lurker on this sub and wanted to share a fun project I created: Concur, a lightweight C# library for Go-inspired concurrency patterns.
Ever since IAsyncEnumerable<T> was released, I've been using it more in new projects. However, I found myself repeatedly writing the same boilerplate code for task synchronization. I wanted a simpler, more user-friendly API, similar to Go's goroutines.
The goal of the API is to mimic the behavior of the go keyword in Golang as closely as possible.
var wg = new WaitGroup();
var channel = new DefaultChannel<int>();
Func<IChannel<int>, Task> publisher = static async ch =>
{
for (var i = 0; i <= 100; i++)
{
await ch.WriteAsync(i);
}
};
_ = Go(wg, publisher, channel);
_ = Go(wg, publisher, channel);
_ = Go(wg, publisher, channel);
// and then close the channel.
_ = Go(async () =>
{
await wg.WaitAsync();
await channel.CompleteAsync();
});
var sum = await channel.SumAsync();
I'd love to hear what you think!
Posted Jul 8, 2025, 4:51 PM UTC
3517