0

I have an old project that was using .net 4.5 which I have upgraded to .net 4.6.2. I am using visual studio 2017, so it should support lambda expressions, but when I try to run my project it always throws this error:

Invalid token '=>' in class, struct, or interface member declaration

How can I get my project to compile using c# 6? I thought just upgrading the target framework was enough?

r3plica
  • 13,017
  • 23
  • 128
  • 290

3 Answers3

2

You need to set the Advanced Build Settings most likely

RightClick Project -> Properties -> Build -> Advanced build settings

Set the Language Version to your favorite or something appropriate

enter image description here

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • By default, the version was set to use lastest Major version, but when changing my version I got a new error. – r3plica Jan 17 '19 at 09:45
0

Try Adding the reference for Microsoft.Net.Compilers to the said project using Nuget (specific for 2.10 or above) to force compiler bindings to use 4.6+ features.

Invalid token class error usually appears when .Net Compilers are not correctly configured in Project.

Install-Package Microsoft.Net.Compilers -Version 2.10.0

Note: If you're using VS 2017, it should automatically do so.

  • I had migrated one of the projects running on .net 4.5 to .net 4.7 and was able to use the features such as Token lambdas without any issue. Note: I have been using VS 2015 and able to migrate & publish it successfully. – Noman Avasthi Jan 17 '19 at 10:01
0

By changing the default language version, I started getting another error:

Invalid option '7' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6

I investigated this and found some answers, which lead me to the correct answer. The only thing I had to do was update the package

Microsoft.Net.Compilers

It was using version 1.3, I upgraded to 2.10.0 and everything worked.

r3plica
  • 13,017
  • 23
  • 128
  • 290