I'm trying to launch a task in C# that both takes a parameter and returns a value, but I can't seem to get the syntax right.
Here's as close as I have gotten: here's a task that is expected to return an int. I'm my lambda, I'm also showing it taking a single parameter, o:
Task<int> task1 = Task.Factory.StartNew<int>((o) => { return 2 ; }, 3);
Console.WriteLine(task1.Result); // prints 2
The above line works (it returns a hardcoded value of 2, but you can see it's doing nothing with the parameter o, making it useless. If I do something with the parameter o, like this:
Task<int> task1 = Task.Factory.StartNew<int>((o) => { return (2 * o) ; }, 3);
I get a syntax message that Delegate 'System.Func' does not take 1 arguments.
Any help on how to achieve both things (pass a parameter and retrieve a value) from a task would be great!