简体   繁体   English

在C#中需要private修饰符

[英]Need of private modifier in C#

I am new to C#. 我是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# 这使我感到困惑。如果我们构建一些项目。为什么我们有私有数据成员。尽管我们根据要求访问了所有内容。没有人可以访问我们的代码。那么C#中需要有private修饰符

Because one of the cornerstones in Object Oriented Programming is encapsulation . 因为encapsulationObject Oriented Programming的基石之一。 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. 通过将逻辑划分为几种私有方法而不是一种大型公共方法,可以遵循模式“ Single Responsible Patterns ,在此Single Responsible Patterns下可以获得更易于维护的健壮代码。

Please start here to understand the basics in OOP. 请从这里开始了解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. 尽管可以使用公共方法或属性来访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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