Why doesn\'t this run:
class Program
{
static void Main(string[] args)
{
Apple a = new Apple(\"green\");
}
}
class Apple
{
I think what you're looking for is this, which keeps your internal variable protected by only exposing a GET to the outside world. For extra safety you could mark _colour as readonly so that it can't be changed within the class itself either (after instantiation), but I think that's overkill. What if your apple gets old and needs to 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;
}
}