← Foundations path
.NETFoundations 18 / 37

Task: a result that comes later

“What is a Task in C#?”

An object that stands for work that has not finished yet. Like an order slip: you hold it now, the result comes later.

See it

1/8A Task is an object that stands for work that will finish later.

In plain words

Some work takes time and happens outside your code: a database query, a call to another API, reading a file. A method that starts such work does not make you wait for it. It hands you a straight away. A is an object, like an order slip at a counter: it says "your result is coming". Later the Task finishes. Then it holds the result (a holds a Loan), or the error if the work failed. You get the result out with . A plain , with no angle brackets, holds no value: it only tells you the work is done.

Say it in the interview

In the lending API

A repository method from the lending API, written in two steps so you can see the Task. Press Next line to walk through it.

Lending.sln · where this file sits
  • src/Infrastructure/Servicing/
Diagram · step 1A Task is an object that stands for work that will finish later.
LoanRepository.cs · line 4 · 1 of 6A primary constructor (C# 12): the passes in this request's when it builds the repository. is the class that calls below.

What they ask next

Interviewers check that you can explain every line. Tap a question to open it.

Traps

Read more: Task asynchronous programming model (Microsoft Learn) · Task class