Task/ValueTask and the async state machine (concepts)
Understand Task results and the async state machine idea. Write tiny async methods, compose awaits, and see a manual TaskCompletionSource.
Task/ValueTask and the async state machine (concepts) is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Mental model
Aim: Build intuition for async.
- Task represents ongoing work and a future result
- await pauses and later resumes
- Compiler creates a state machine
- ValueTask: newer optimization (concept only here)
Async method basics
An async method returns Task<T> immediately; later it completes with the value when awaited work finishes.
using System;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
// Tiny async method: returns Task<int>
static async Task<int> GetAnswerAsync()
{
// Offload simple work to a thread-pool task (demo only)
int value = await Task.Run(() =>
{
Thread.Sleep(200); // simulate work
return 42;
});
return value; // completes Task<int>
}
public static void Main(string[] args)
{
// In C# 6, Main cannot be async; block just for demo (console apps are safe here)
int result = GetAnswerAsync().Result;
Console.WriteLine("Answer = " + result);
}
}
Composing awaits
Await lets you write sequential async steps in a clear, top-down style.
using System;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
static async Task<int> Step1Async()
{
// pretend I/O
await Task.Delay(100);
return 10;
}
static async Task<int> Step2Async(int x)
{
await Task.Delay(100);
return x * 3; // simple transform
}
static async Task<int> PipelineAsync()
{
int a = await Step1Async(); // await first
int b = await Step2Async(a); // then await second
return a + b; // total 10 + 30 = 40
}
public static void Main(string[] args)
{
int total = PipelineAsync().Result; // block in demo
Console.WriteLine("Total = " + total);
}
}
State machine idea
Concept: The compiler turns an async method into a state machine.
- Locals are stored as fields
- Each await yields a new state
- When the awaited Task completes, a continuation resumes the method
- Exceptions are captured and set on the Task
TaskCompletionSource demo
TaskCompletionSource lets you create a Task and signal its completion later; await consumes it like any other Task.
using System;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
// Emulate an async source that completes later
static Task<string> MakeTaskManually()
{
var tcs = new TaskCompletionSource<string>();
// Complete on a worker after a short delay
ThreadPool.QueueUserWorkItem(_ =>
{
Thread.Sleep(150);
tcs.SetResult("done");
});
return tcs.Task;
}
static async Task<string> UseAsyncSource()
{
string s = await MakeTaskManually(); // await a Task you created
return s.ToUpperInvariant();
}
public static void Main(string[] args)
{
Console.WriteLine(UseAsyncSource().Result);
}
}
ValueTask concept
ValueTask (concept): a newer, allocation-saving return type for frequently synchronous results.
- Useful when results complete already available most of the time
- Has rules to avoid extra allocations
- Not available in C# 6—use Task here; the ideas still apply
Async state machine
Recap
Recap: Task models work; await pauses and resumes; the compiler builds a state machine. ValueTask is an optimization concept (use Task in C# 6).
Frequently asked questions
Is the “Task/ValueTask and the async state machine (concepts)” lesson free?
Yes — the full text of “Task/ValueTask and the async state machine (concepts)” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Task/ValueTask and the async state machine (concepts)”?
Understand Task results and the async state machine idea. Write tiny async methods, compose awaits, and see a manual TaskCompletionSource. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Task/ValueTask and the async state machine (concepts)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Task/ValueTask and the async state machine (concepts)
- Async pitfalls: sync-over-async and deadlocks
- ConfigureAwait, exception flow