The C# method Task.Factory.StartNew
takes an optional cancellation token as a second argument. In the sample code below, I've included this argument. What I don't get is that if don't include the second argument, the code still works. Apparently, the action has access to the token even if it is not passed as an argument. So what's the purpose of passing it as an argument? Is there a situation in which the second argument would be needed or was it totally unnecessary for Microsoft to include this option?
var cts = new CancellationTokenSource();
var token = cts.Token;
Task.Factory.StartNew(() =>
{
int i = 0;
while (true)
{
token.ThrowIfCancellationRequested();
Console.WriteLine($"{i++}\t");
Thread.Sleep(500);
}
}, token);
Console.ReadKey();
cts.Cancel();