1

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.

  • You would not want that method to be made public and gets used by other class accidentally or by mistake. A property would be clean way to exchange or pass the information with the class. If you are sure and aware that the method should be called from outside purposefully, then it is better to stick public modifier with it. – Karan May 13 '15 at 8:31
  • 3
    That toturial sounds outdated. You should check up on auto properties which solves this problem. – Esben Skov Pedersen May 13 '15 at 8:33
  • possible duplicate of What is the benefit of using getters and setters on properties in C#? – gnat May 13 '15 at 9:01
  • 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. Hope it's clear to you. – wsDev May 13 '15 at 9:05
  • 2
    This seems like a pretty classic example of a tutorial teaching the solution to a problem long before the problem itself has actually arisen. – Ben Aaronson May 13 '15 at 9:07
0

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

6

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.

3

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:

0

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.