9

I need to understand about Func type expression deeper.

public class TheResult : IResultEntry {
    ...
}

With above class, why does below second method require cast?

I can read the error message of course, but hard to understand.

// Success
public Task<IResultEntry> ProcessAsync_1()
{
    return Task.Factory.StartNew(() => (IResultEntry) new TheResult());
}

// Fail: Compiler error. Cannot implicitly convert...
public Task<IResultEntry> ProcessAsync_2()
{
    return Task.Factory.StartNew(() => new TheResult());
}

If we change it to named method with help of ReSharper, we can do without a cast.

public Task<IResultEntry> ProcessAsync_2_Changed()
{
    return Task.Factory.StartNew(function);
}

private IResultEntry function()
{
    return new TheResult();
}
Youngjae
  • 24,352
  • 18
  • 113
  • 198

0 Answers0