I am going through the beginners tutorial for C# from a website
http://www.homeandlearn.co.uk/csharp/csharp_s10p1.html
On chapter of classes, it makes the method of class private and then use property to get and set values. I am unable to understand why I need to make anything private in my class if I have to use/reuse it throughout the projects?
I am not asking for getters and setters, I am more concern about why I need to use Private keyword if all things are working fine with Public in tutorial example.
5
The most important reason for limiting public access is to reduce the coupling in your application. By only exposing a select set of public methods or properties, you limit the number of connections between different classes.
There is widespread confusion about this, where many people believe that the purpose of private access modifier is to prevent other classes from accidentally breaking your class or data, like some kind of security or data protection. That is at most a very minor benefit in rare circumstances.
The real purpose is to reduce coupling, which in turn makes it easier to make changes to the code later.
Read up on Encapsulation, Indirection, Single responsibility
The author provided a simple example which, due to its simplicity might make it more difficult for one to understand the importance of private methods.
Private methods are a way in which you can encapsulate and break down the behavior of a given object without exposing them to the outside.
Let us say that we have an object of type Person
:
public class Person
{
public string Name {get; set;}
public int Age {get; set;}
}
In the above, there is nothing stopping me from creating a person with a negative age:
Person p = new Persion() {Name = "Bob", Age = -1}
I could have a method to ensure that the age is within a set of established ranges.
public class Person
{
private int age;
public string Name {get; set;}
public int Age {get {return this.age}; set {this.ValidateAge(value);}
public void ValidateAge(int age) { if(age > 0) this.age = age; }
}
The problem now is that I have a ValidateAge
method which is exposed to who ever consumes the Person
class. This exposes an internal mechanism of the class and also provides a means to someone to change the internal data of the class without going through the appropriate property, thus breaking encapsulation.
Making the method private will hide it from outside callers, thus giving you better control over how and when are properties changed.
The tutorial is trying to teach you about encapsulation.
Encapsulation is an important part of Object Oriented Programming. It’s a technique programmers use to keep their large codebases clean and easy to work with. Essentially, it means that anything that can be left as a private implementation of a class should be left private. This reduces the “public surface” of your class, and helps keep your application responsibilities isolated into individual classes.
You can read up on this on wikipedia:
- Ecapsulation
- related: SOLID
The tutorial appears to be demonstrating what you can do, not giving examples of what you should do.
Specifically in this case, their use of properties means b.MyProperty
below doesn’t equal "Bob"
, which would fail code review in any shop I’ve worked in.
HappyBirthday a = new HappyBirthday();
a.MyProperty = "Bob";
a.MyProperty = a.MyProperty;
HappyBirthday b = new HappyBirthday();
b.MyProperty = a.MyProperty;