When I write the following code:
Task<string> task = Task.Factory.StartNew<string>(() => "first task")
.ContinueWith(t =>
{
Console.WriteLine(t.Result);
Console.WriteLine("second task");
});
That is wrong!
Then I change it to this:
var task = Task.Factory.StartNew<string>(() => "first task")
.ContinueWith(t =>
{
Console.WriteLine(t.Result);
Console.WriteLine("second task");
});
Then everything is OK!
Why?
What is the different between “Task task” and “var task”?
Your line of code returns a
Task, not aTask<string>object, because you wroteContinueWith, notContinueWith<string>.A tip that could help you in the future: when you replace a type by
varin a variable declaration, you can move your mouse over thevarkeyword in Visual Studio, a pop-up will be shown with the actual type thevarhides in your code.