1

It would seem I've come across a considerable lack in my understanding. I understand it is best practice to use a private variable within a class and access it via a public getter outside said class. Now when using C#s default getter method (private string Image { get; }) I cannot access the Image variable outside of this class (Console.WriteLine(items[i].Image);).

enter image description here

Although I could write a custom public getter, this seems absurd since having a private getter on a private variable that does nothing other than return the variable seems utterly redundant and thus makes me think I'm missing something.

Jonathan Woollett-light
  • 1,491
  • 2
  • 17
  • 27
  • 1
    If you don't want it to be private, why did you make it private? If you want it to be public, why didn't you make it public? – Servy Oct 16 '17 at 21:42
  • 2
    Yes, your "getter" needs to be public. public string Image {get; } – Inisheer Oct 16 '17 at 21:43
  • @Servy I clearly say this in my question. 'I understand it is best practice to use a private variable within a class and access it via a public getter outside said class' – Jonathan Woollett-light Oct 16 '17 at 21:43
  • @Inisheer Does that not merely make the Image variable public? – Jonathan Woollett-light Oct 16 '17 at 21:44
  • 2
    @JonathanWoollett-light You said that, and then wrote a private getter...even though in the question you say that you know that the correct thing to do is to not have a private getter... – Servy Oct 16 '17 at 21:44
  • @Servy In my declaration of Image I made Image private, I did not think this applied to its getter. As I outline in my question I beleive that would make the getter entirely useless. – Jonathan Woollett-light Oct 16 '17 at 21:45
  • 1
    @JonathanWoollett-light The accessibility modifier of the property applies to the whole property. You are correct in saying that this private property seems to be useless, as you've written it. – Servy Oct 16 '17 at 21:47
  • This may help https://stackoverflow.com/questions/6001917/what-are-automatic-properties-in-c-sharp-and-what-is-their-purpose – hatchet - done with SOverflow Oct 16 '17 at 21:47
  • 2
    The fundamental problem here is that you've completely confused variables with properties. Variables are not properties and properties are not variables. Fields -- class-level variables -- should almost always be private. Properties should be as private or public as they need to be, and getters and setters of properties can have different accessibilities. There's no contradiction there; properties are not fields. Properties are *logical properties of an object that might be implemented as a field*. Fields are implementation details, sometimes of properties. – Eric Lippert Oct 16 '17 at 22:21

2 Answers2

4

When you declare

private string Image { get; }

you make a private read-only property, meaning that both the getter and the setter are private. In addition, the setter is inaccessible outside constructors / initializers.

Changing it to

public string Image { get; }

would give you a public read-only property. This roughly corresponds to the following arrangement:

  • The getter is public, i.e. accessible to everyone inside and outside the class
  • The setter is private*, i.e. accessible only inside the class.
  • The property is read-only, so the additional restriction on the private setter is that it must be accessed through a constructor or an initializer.

In Java I would create a private variable and a public getter. Is this effectively the version of that in C#?

An equivalent of this in Java would be a private final field, because C# restricts access to setter outside the constructor.

* Actually, there is no setter for read-only properties. Assignments write directly to backing storage for the property, without going through a setter. The distinction between having a private setter accessible only from a constructor and having no setter at all becomes important if you try writing a property through reflection.

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
  • In Java I would create a private variable and a public getter (which I beleive to be best practice), is this effectively the version of that in C#? – Jonathan Woollett-light Oct 16 '17 at 21:53
  • @JonathanWoollett-light That's almost right (see the edit). A private non-final variable would be equivalent to `public Image {get; private set;}` – Sergey Kalinichenko Oct 16 '17 at 21:58
  • Auto-implemented get only properties do not have setter. Constructor write to backing read only field directly. – user4003407 Oct 16 '17 at 22:13
  • @PetSerAl That's right. This is why I said "roughly corresponds", because OP is trying to transfer his knowledge of Java to C#, so I didn't want to cause an additional confusion. I added a footnote to explain the difference. – Sergey Kalinichenko Oct 16 '17 at 22:15
0

Either you declare a private member:

private string _image;

Or a public property. When you declare a property, the getter and setter have their own privacy modifiers (i.e protected/public/private/none). For example:

public string Image { get; protected set; }
Yosi Dahari
  • 5,936
  • 5
  • 20
  • 40