Implementing read-only properties with {get; }

Why this launch fails:

  class Program
  {
    static void Main(string[] args)
    {
      Apple a = new Apple("green");
    }
  }

  class Apple
  {

    public string Colour{ get; }

    public Apple(string colour)
    {
      this.Colour = colour;
    }

  }

      

+1


source to share


4 answers


Your code is valid for C # 6 that ships with Visual Studio 2015. It is not valid for previous versions of the language or Visual Studio. You can technically install the old Roslyn preview in VS 2013, but now it's not worth it that VS 2015 was released.

For this issue to occur, either you are using the wrong version of Visual Studio to compile your C # 6 code, or you are trying to compile your code from the command line using the wrong development environment -ie, your PATH points to the old compiler. Perhaps you opened "Developer Command Prompt for 2013" instead of 2015?

You must either compile your code with Visual Studio 2015 or make sure your path variable points to the latest compiler.

If you need to use Visual Studio 2013 or older, you will have to change your code to use the old syntax, for example:

public readonly string _colour;

public string Colour { get {return _colour;}}

public Apple(string colour)
{
    _colour=colour;
}

      



or

public string Colour {get; private set;}

public Apple(string colour)
{
    Colour=colour;
}

      

Note that the second option is not truly read-only, other members of the class may still modify the property

Note

You can use Visual Studio 2015 to target .NET 4.5. Language and runtime are two different things . The real requirement is that the compiler must match the language version

+7


source


Or add a private setter to the property:

public string Colour{ get; private set;}

      



Or add a read-only backlink field:

private string _colour;
public string Colour{ get return this._colour; }

public Apple(string colour)
{
  this._colour = colour;
}

      

+3


source


You have several options here:

// Make the string read only after the constructor has set it
private readonly string colour
public string Colour { get { return colour; } }

public Apple(string colour)
{
  this.colour = colour;
}


// Make the string only readonly from outside but editing from within the class
public string Colour { get; private set; }

public Apple(string colour)
{
  this.Colour= colour;
}

      

+1


source


I think what you are looking for is exactly this, which protects your inner variable by only exposing the GET to the outside world. For added security, you can mark _colour as readonly so that it cannot be changed inside the class itself either (after instantiation), but I think this is overkill. What if your apple turns old and should turn brown?

class Program
{
    static void Main(string[] args)
    {
        Apple a = new Apple("green");
    }
}

class Apple
{
    private string _colour;
    public string Colour
    {
        get
        {
            return _colour;
        }
    }

    public Apple(string colour)
    {
        this._colour = colour;
    }

}

      

+1


source







All Articles