c# - Implementing read-only properties with { get; } -


why doesn't run:

  class program   {     static void main(string[] args)     {       apple = new apple("green");     }   }    class apple   {      public string colour{ get; }      public apple(string colour)     {       this.colour = colour;     }    } 

your code valid c# 6, comes visual studio 2015. not valid previous versions of language or visual studio. technically, can install old pre-release version of roslyn in vs 2013 isn't worth trouble vs 2015 released.

for problem occur, either using wrong version of visual studio compile c# 6 code, or trying compile code command line using wrong development environment -ie, path points old compiler. perhaps opened 'developer command prompt 2013' instead of 2015?

you should either compile code using visual studio 2015, or ensure path variable points latest compiler.

if have use visual studio 2013 or older, you'll have change code use older syntax, eg:

public readonly string _colour;  public string colour { {return _colour;}}  public apple(string colour) {     _colour=colour; } 

or

public string colour {get; private set;}  public apple(string colour) {     colour=colour; } 

note second option isn't read-only, other members of class can still modify property

note

you can use visual studio 2015 target .net 4.5. language , runtime are 2 different things. real requirement compiler must match language version


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -