内部访问修饰符与私有访问修饰符

发布于 2024-09-25 09:35:06 字数 69 浏览 9 评论 0原文

C# 中的 internalprivate 访问修饰符有什么区别?

What is the difference between the internal and private access modifiers in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

追星践月 2024-10-02 09:35:06

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).

虐人心 2024-10-02 09:35:06

私有:私有成员只能在自己的类型(Own class)内访问。

内部:内部成员只能在程序集中通过继承(其派生类型)或通过类的实例进行访问。

在此处输入图像描述

来源: dotnetbull - 什么是访问C# 中的修饰符

Private: Private members are only accessible within the own type (Own class).

Internal: Internal member are accessible only within the assembly by inheritance (its derived type) or by instance of class.

enter image description here

Source: dotnetbull - what is access modifier in c#

谎言月老 2024-10-02 09:35:06

内部成员对于声明它们的程序集中的所有代码都是可见的。
(以及使用 [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 like private 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)

十秒萌定你 2024-10-02 09:35:06

内部成员可在程序集中访问(只能在同一项目中访问)

私有成员可在同一类中访问

初学者示例

解决方案中有 2 个项目(Project1、Project2),Project1 有一个引用到项目2。

  • Project2 中编写的公共方法将在 Project2 中可访问,而
  • Project2 中编写的 Project1 内部方法只能在 Project2 中访问,而不能在 Project1 中访问
  • 。 Project2 的 class1 中编写的私有方法只能被同一个类访问。它既不能在项目 2 的其他类中访问,也不能在项目 1 中访问。

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.

  • Public method written in Project2 will be accessible in Project2 and the Project1
  • Internal method written in Project2 will be accessible in Project2 only but not in Project1
  • private method written in class1 of Project2 will only be accessible to the same class. It will neither be accessible in other classes of Project 2 not in Project 1.
娇俏 2024-10-02 09:35:06

私有成员只能在类的主体或声明它们的结构中访问。

内部类型或成员只能在同一程序集中的文件内访问

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

向地狱狂奔 2024-10-02 09:35:06

私有 - 类/范围/结构等中的封装。

内部 - 封装在程序集中。

private - encapsulations in class/scope/struct ect'.

internal - encapsulation in assemblies.

时光礼记 2024-10-02 09:35:06

内部将允许您在多个业务逻辑类之间引用数据访问静态类(用于线程安全),同时不订阅它们以在连接池中相互继承该类/行程,并最终避免允许 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文