Chamanjit Kaur
Chamanjit Kaur

Reputation: 51

Need of private modifier in C#

I am new to C#.
This makes me confusing.If we building some project.Why we have private data members.Although everything is accessed by us according to our requirement.Nobody have access to our code.Then what is the need of private modifiers in C#

Upvotes: 0

Views: 91

Answers (2)

Arun Prasad
Arun Prasad

Reputation: 360

The Link provides you the necessary details about why should we use private for fields. We declare private for the fields to stop processing it functionalities in other classes. Although it could be accessed using public methods or properties.

Upvotes: 0

Hypnobrew
Hypnobrew

Reputation: 1140

Because one of the cornerstones in Object Oriented Programming is encapsulation. This mean you hide implementation details not concerned by any outside logic.

By dividing logic into several private methods instead of one big public method, you follow the pattern Single Responsible Patterns where you get robust code which is easier to maintain.

Please start here to understand the basics in OOP.

Upvotes: 2

Related Questions