简体   繁体   中英

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#

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM