Why I need to make method of my class private, If I need to access it throughout my application?

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;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật