0

Having the following simple class:

class Animal
{
    public string Type => "Animal";
}

This does not compiles for the arrow operator saying ; expected. I was trying to do something like:

class Animal
{
    public string Type { get { return "Animal"; } }
}

Is it because I am using Visual Studio 2012? Is it possible that in this case make the first version of code compile, without installing another version of VS?

sstan
  • 35,425
  • 6
  • 48
  • 66
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76

1 Answers1

1

Your first code sample is using a new C# syntax that allows property getter bodies to be defined as a lambda expression. This is a new feature introduced in C# 6, which is only available beginning with Visual Studio 2015.

If you look at this other thread, you'll see that Visual Studio 2012 only supports up to C# 5.

Community
  • 1
  • 1
sstan
  • 35,425
  • 6
  • 48
  • 66
  • "Visual Studio 2015+" since there are no other versions yet and it looks like the + belongs to the version I recommend removing it unless you already have access to Visual Studio 2020 of course. – Hatted Rooster Sep 25 '16 at 14:24
  • @Gill: The [preview edition of Visual Studio 15](https://www.visualstudio.com/en-us/news/releasenotes/vs15-relnotes) is already available, which showcases support for C# 7. Still, I'll remove it. – sstan Sep 25 '16 at 14:26
  • okay, but considering the most voted answer in [this topic](http://stackoverflow.com/questions/19532942/which-version-of-c-sharp-am-i-using) if one is building in .net 4.6 shouldn't have available C# 6 features also? Because I changed target framework to .Net 4.6 and I still get the same error. I know you have said that C# 6 is introduced in VS2015 but I was wondering if it can be used in 2012 versions too – meJustAndrew Sep 25 '16 at 14:46
  • @meJustAndrew: It may be possible. Not sure how well it would work though. Have a look [in this thread](http://stackoverflow.com/questions/27093908/how-to-enable-c-sharp-6-0-feature-in-visual-studio-2013) for possible ways of getting C# 6 to work in an older Visual Studio release. – sstan Sep 25 '16 at 14:54
  • Thank you very much, this covers much what I was looking for – meJustAndrew Sep 25 '16 at 14:57