-2
public class MyClass
{

    public string DeviceCommands { get; set; } = "DeviceCommands";
}

I have 30 errors on this kind of lines...

Leon Havin
  • 177
  • 2
  • 14

2 Answers2

5

.NET 4.6.2 is a framework version, not a language version. To be able to do that line you must be using C# 6 or newer. If you are compiling with C# 6 you would be allowed to use that syntax in a .NET 2.0 project.

If you are using Visual Studio 2015 or newer you are using C# 6 or newer.

If you are using Visual Studio 2015 and are still getting errors there is some other problem with your code that you are not showing us.

UPDATE:

If you are using VS2015 and are getting a error that says

Error CS8026: Feature 'auto property initializer' is not available in C# 5. Please use language version 6 or greater.

That means in your project properties -> Build -> Advanced screen you set the language version manually to 5 or lower instead of default.

enter image description here

Setting it back to default will fix the problem.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • I am using VS2013 and .NET 4.6.2 is set in the project settings - I see now that in a previous project version it was compiling but now it is not...and can't find any apparent reason... – Leon Havin Feb 01 '17 at 22:57
  • 3
    ***"I am using VS2013..."*** That is your problem. You are using C# 5 because you are using VS2013, you need to open the project in VS2015 to be able to compile it. – Scott Chamberlain Feb 01 '17 at 22:58
0

The syntax you have are Auto Property Initializers.

It has nothing to do with the .NET Version. It is a language feature from C# 6.0.

You need the the roslyn compiler plattform to make this work.

If you can not support C# 6.0, you could do it the old way:

public class MyClass
{
    public string DeviceCommands { get; set; };

    public MyClass()
    {
        DeviceCommands = "DeviceCommands";
    }
}
Graham
  • 7,431
  • 18
  • 59
  • 84
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111