How to construct a Task without starting it?

To use the Task constructor that accepts an object state argument you must have a function that accepts an object argument too. Generally this is not convenient. The reason that this constructor exists is for avoiding the allocation of an object (a closure) in hot paths. For normal usage the overhead of closures is negligible, and avoiding them will complicate your code for no reason. So this is the constructor you should use instead:

public Task (Func<TResult> function);

...with this lambda as argument:

() => GetIntAsync("3")

There is one peculiarity in your case though: the lambda you pass to the constructor returns a Task<int>. This means that the generic type TResult is resolved to Task<int>, and so you end up with a nested task:

var t = new Task<Task<int>>(() => GetIntAsync("3"));

Starting the outer task will result to the creation of the inner task. To get the final result you'll have to use the await operator twice, one for the completion of the outer task, and one for the completion of the inner task:

static async Task Main(string[] args)
{
    var outerTask = new Task<Task<int>>(() => GetIntAsync("3"));
    //...
    outerTask.Start(); // or outerTask.RunSynchronously() to use the current thread
    //...
    Task<int> innerTask = await outerTask; // At this point the inner task has been created
    int result = await innerTask; // At this point the inner task has been completed
}

I find that an async lambda works best, because:

  1. the work is started by calling the method which means you don't need to call .Start, so it's not possible to forget to do it.
  2. this approach doesn't 'suffer' from the inner/outer task problem; you always await the right task, not it's wrapper.
Func<Task<int>> f = async () => await WorkAsync(2);

Console.WriteLine("Sleeping before start");

await Task.Delay(100);

Console.WriteLine("Waking up to start");

var result = await f();

Console.WriteLine($"The work is done and the answer is: {result}");

This results in the following output:

Sleeping before start
Waking up to start
Starting 2
Ending 2
The work is done and the answer is: 4


var t = new Task<int>(() => GetIntAsync("3").Result);

Or

var t = new Task<int>((ob) => GetIntAsync((string) ob).Result, "3");

To avoid using lambda, you need to write a static method like this:

private static int GetInt(object state)
{
   return GetIntAsync(((string) state)).Result;
}

And then:

var t = new Task<int>(GetInt, "3");