0
0
C Sharp (C#)programming~10 mins

Task and Task of T types in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Task that runs a method asynchronously.

C Sharp (C#)
Task myTask = Task.[1](() => Console.WriteLine("Hello from Task"));
Drag options to blanks, or click blank then click option'
AStart
BRun
CExecute
DBegin
Attempts:
3 left
💡 Hint
Common Mistakes
Using Task.Start directly on a delegate instead of creating a Task first.
Confusing Task.Run with Task.Factory.StartNew.
2fill in blank
medium

Complete the code to create a Task that returns an integer result asynchronously.

C Sharp (C#)
Task<int> myTask = Task.[1](() => 42);
Drag options to blanks, or click blank then click option'
ARun
BStart
CExecute
DBegin
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use Task.Start with a lambda directly.
Using Task.Factory.StartNew without specifying the return type.
3fill in blank
hard

Fix the error in the code to await a Task that returns a string.

C Sharp (C#)
async Task<string> GetMessageAsync() {
    return await [1];
}

Task<string> messageTask = Task.FromResult("Hello");
Drag options to blanks, or click blank then click option'
AmessageTask.Result
BmessageTask.GetAwaiter()
CmessageTask
DmessageTask.Wait()
Attempts:
3 left
💡 Hint
Common Mistakes
Awaiting the Result property instead of the Task.
Calling Wait() which blocks instead of awaiting.
4fill in blank
hard

Fill both blanks to create and start a Task that returns the length of a string.

C Sharp (C#)
Task<int> lengthTask = new Task<int>(() => [1].Length);
lengthTask.[2]();
Drag options to blanks, or click blank then click option'
AinputString
BStart
CRun
DExecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using Task.Run instead of new Task and Start.
Forgetting to start the Task after creating it.
5fill in blank
hard

Fill all three blanks to create a Task that returns the uppercase version of a string and start it.

C Sharp (C#)
Task<string> upperTask = new Task<string>(() => [1].[2]());
upperTask.[3]();
Drag options to blanks, or click blank then click option'
Ainput
BToUpper
CStart
DRun
Attempts:
3 left
💡 Hint
Common Mistakes
Using Task.Run instead of new Task and Start.
Calling the method without parentheses.
Not starting the Task.