1

The following code gives an error

Not all code paths return a value in lambda expression of type 'System.Func'.

It highlights line =>. Not sure why?

    var ui = new DataflowBlockOptions();
    ui.TaskScheduler = TaskScheduler.Current;
    ui.BoundedCapacity = 1;
    ui.MaxMessagesPerTask = 1;

    ActionBlock<string> tradeSignalLog = new ActionBlock<string>(line => 
        {
            Console.WriteLine(line);
        }, ui);
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Ivan
  • 7,448
  • 14
  • 69
  • 134

2 Answers2

2

The original error is that the overload resolution failed. Then the C# compiler has some heuristics that try to figure out why the overload resolution failed, and in this case those heuristics didn't tell you the "correct" cause.

First look at the available two parameter overloads:

public ActionBlock(Action<TInput> f, ExecutionDataflowBlockOptions o);
public ActionBlock(Func<TInput, Task> f, ExecutionDataflowBlockOptions o);

The second parameter is ExecutionDataflowBlockOptions in both cases. But you supply a DataflowBlockOptions which is a base class of ExecutionDataflowBlockOptions. Since base classes are not implicitly convertible to derived classes the overload resolution fails. Once you create the correct kind of options, your code will work.

A related answer of Eric Lippert on compiler-error heuristics when overload resolution fails: Passing lambda functions as named parameters in C#

Community
  • 1
  • 1
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
0

Try creating,

Action<string> action = line => Console.WriteLine(line);
ActionBlock<string> tradeSignalLog = new ActionBlock<string>(action, ui);

See if that doesn't fix your problem. It seems the compiler is interpreting your code and expecting a Func<> as per: http://msdn.microsoft.com/en-us/library/hh194684(v=vs.110).aspx

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69