内部访问修饰符与私有访问修饰符
C# 中的 internal
和 private
访问修饰符有什么区别?
What is the difference between the internal
and private
access modifiers in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
internal 用于程序集范围(即只能从同一 .exe 或 .dll 中的代码访问)
private 用于类范围(即只能从同一类中的代码访问) 。
internal is for assembly scope (i.e. only accessible from code in the same .exe or .dll)
private is for class scope (i.e. accessible only from code in the same class).
来源: dotnetbull - 什么是访问C# 中的修饰符
Source: dotnetbull - what is access modifier in c#
内部
成员对于声明它们的程序集中的所有代码都是可见的。(以及使用
[InternalsVisibleTo ]
属性)private
成员仅对声明类可见。 (包括嵌套类)外部(非嵌套)类不能声明为
private
,因为没有包含范围可以将其设为私有。为了回答您忘记问的问题,受保护的成员类似于私有成员,但在继承声明类型的所有类中也可见。 (但仅限于至少当前类类型的表达式)
internal
members are visible to all code in the assembly they are declared in.(And to other assemblies referenced using the
[InternalsVisibleTo]
attribute)private
members are visible only to the declaring class. (including nested classes)An outer (non-nested) class cannot be declared
private
, as there is no containing scope to make it private to.To answer the question you forgot to ask,
protected
members are likeprivate
members, but are also visible in all classes that inherit the declaring type. (But only on an expression of at least the type of the current class)内部成员可在程序集中访问(只能在同一项目中访问)
私有成员可在同一类中访问
初学者示例
解决方案中有 2 个项目(Project1、Project2),Project1 有一个引用到项目2。
internal members are accessible within the assembly (only accessible in the same project)
private members are accessible within the same class
Example for Beginners
There are 2 projects in a solution (Project1, Project2) and Project1 has a reference to Project2.
私有成员只能在类的主体或声明它们的结构中访问。
内部类型或成员只能在同一程序集中的文件内访问
Private members are accessible only within the body of the class or the struct in which they are declared.
Internal types or members are accessible only within files in the same assembly
私有 - 类/范围/结构等中的封装。
内部 - 封装在程序集中。
private - encapsulations in class/scope/struct ect'.
internal - encapsulation in assemblies.
内部将允许您在多个业务逻辑类之间引用数据访问静态类(用于线程安全),同时不订阅它们以在连接池中相互继承该类/行程,并最终避免允许 DAL 类促进公众层面的获取。这在设计和最佳实践方面有无数的支持。
实体框架充分利用了这种类型的访问
Internal will allow you to reference, say, a Data Access static class (for thread safety) between multiple business logic classes, while not subscribing them to inherit that class/trip over each other in connection pools, and to ultimately avoid allowing a DAL class to promote access at the public level. This has countless backings in design and best practices.
Entity Framework makes good use of this type of access