3

This code runs on Visual Studio 2019 but fails to compile on Visual Studio 2012

Task.Factory.StartNew(HandleMessages);
public async Task HandleMessages()
{
   ...
}

Visual Studio 2012 shows this error

The call is ambiguous between the following methods or properties: 'System.Threading.Tasks.TaskFactory.StartNew(System.Action)' and 'System.Threading.Tasks.TaskFactory.StartNew<System.Threading.Tasks.Task>(System.Func<System.Threading.Tasks.Task>)'

I can fix this error like this :

Task.Factory.StartNew<Task>(HandleMessages);

But I can't understand why does Visual Studio 2019 compile it without showing any error while Visual Studio 2012 rejects it.

I'm building an application using the following:

  • Visual Studio Community 2019
  • WinForms
  • C# 5.0
  • .Net Framework 4.5

I have the same setup for the Visual Studio 2012

Tried building the solution using MSBuild it shows the same error that Visual Studio 2012 does.

I need to know why does Visual Studio 2019 accepts this code and if there is a way to make it behave the same way Visual Studio 2012 does.

  • 2
    Visual Studio prior to 2015 uses the old compiler, whereas 2015+ uses Roslyn. It looks like the overload resolution rules were improved in Roslyn - it's not really a "breaking change", since the code wouldn't compile previously. – Richard Deeming Nov 24 '21 at 09:06
  • 3
    Previously you had the error due to https://stackoverflow.com/a/10034429/11683. Then you stopped getting the error due to https://stackoverflow.com/a/45236746/11683 (specifically the *"when passing method groups (as opposed to lambdas) to overloads expecting delegates"* part). I would expect the new rules to not apply when you choose C# v5.0 explicitly in the project settings, but apparently that controls only the available language features, not the overload resolution rules. – GSerg Nov 24 '21 at 09:07
  • 1
    ... (which is apparently [hinted by](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#c-language-version-reference) *"The compiler accepts only syntax that is included in version 5"* as opposed to "The compiler behaves as if it was version 5") – GSerg Nov 24 '21 at 09:15
  • That was very helpful, but I need MSBuild and Visual Studio both to show an error or allow the code otherwise it will only cause more issues for me – Saadat Al-KayyaLi Nov 24 '21 at 09:19
  • 1
    That's why it's called a "breaking change". If you need to use two VS versions, use those that are on the same side of the breaking change. I guess you could also manually point MSBuild to the compiler version included with VS 2012. – GSerg Nov 24 '21 at 09:25

0 Answers0