简体   繁体   English

为什么C#允许方法/成员在类是内部时公开

[英]Why C# allows methods/members to be public when the class is internal

I have a class which I marked as internal and I marked fields and methods as public. 我有一个我标记为内部的类,我将字段和方法标记为公共。 It compiled without errors or warnings. 它编译时没有错误或警告。 Is there any specific need to have methods as public and class as internal (except when they are being implemented from interfaces or classes)? 是否有任何特定的需要将方法作为public和class作为内部(除非它们是从接口或类实现的)?

It has no adverse effects, and also means that if you ever do decide to make the type public, you won't need to change the accessibility of your members. 它没有任何不利影响,也意味着如果您决定公开该类型,则无需更改成员的可访问性。

Basically for a member: 基本上对于会员:

  • public means the member is visible to anyone who can see the Type. public表示任何可以看到Type的人都可以看到该成员。

  • internal means the member is only visible in the current assembly, even if the Type is publicly visible. internal表示该成员仅在当前程序集中可见,即使Type是公开可见的。

So your choice would be based on which of these is most appropriate. 所以你的选择将基于哪一个是最合适的。 In general it's most appropriate to make the members public (ie visible to anyone who can see the Type, ie part of the Type's public API). 一般来说,将成员公开是最合适的(即任何可以看到Type的人都可以看到,即Type的公共API的一部分)。 You would make members internal for the same reason you make members internal in a public class - typically helper members that should only be visible to "friend" classes in the same assembly, and don't form part of the public API. 您可以将成员设置为内部,原因与您在公共类中创建内部成员的原因相同 - 通常是辅助成员,只应对同一程序集中的“friend”类可见,并且不构成公共API的一部分。

In addition, an internal Type can derive from a public Type, so can inherit and override public members. 此外,内部类型可以从公共类型派生,因此可以继承和覆盖公共成员。 Would it make sense to allow overridden public members, but not new public members? 允许被覆盖的公众成员,而不是新公共成员是否有意义?

A class marked as internal with public methods would allow for other classes in the same assemly to reference those methods. 标记为公共方法内部的类将允许同一组件中的其他类引用这些方法。

internal (C# Reference) 内部(C#参考)

A common use of internal access is in component-based development because it enables a group of components to cooperate in a private manner without being exposed to the rest of the application code. 内部访问的一个常见用途是基于组件的开发,因为它使一组组件能够以私有方式进行协作,而不会暴露给其他应用程序代码。 For example, a framework for building graphical user interfaces could provide Control and Form classes that cooperate using members with internal access. 例如,用于构建图形用户界面的框架可以提供使用具有内部访问权限的成员进行协作的Control和Form类。 Since these members are internal, they are not exposed to code that is using the framework. 由于这些成员是内部成员,因此它们不会暴露给使用该框架的代码。

It is an error to reference a type or a member with internal access outside the assembly within which it was defined. 引用具有内部访问权限的类型或成员在组件定义之外是错误的。

Edit 编辑

From Scope of internal method in C#? C#内部方法的范围?

You may want a method on a public class which can be called by other classes in the assembly, but not by clients of the assembly. 您可能需要公共类上的方法,该方法可以由程序集中的其他类调用,但不能由程序集的客户端调用。

There's no warning because it doesn't have any adverse effects. 没有任何警告,因为它没有任何不利影响。 The members won't be accessible from other assemblies anyway, whether you mark them as public or internal. 无论如何,无论您将其标记为公共成员还是内部成员,都无法访问其他成员。

Consider this case: 考虑这种情况:

You have a FooFactory creating objects implementing IFoo. 你有一个FooFactory创建实现IFoo的对象。 Your IFoo implementations may be internal because you don't want to expose them to other assemblies that just use IFoo. 您的IFoo实现可能是内部的,因为您不希望将它们暴露给仅使用IFoo的其他程序集。 The members of the IFoo implementation must be public so they can be used in the other assemblies. IFoo实现的成员必须是公共的,以便可以在其他程序集中使用它们。

So, as I've now read all of your question, I don't think there's any reason for methods on internal classes to be public unless there's a way for those classes to be accessed from other assemblies either through interfaces or base classes. 所以,正如我现在已经阅读了你的所有问题,我认为内部类的方法没有任何理由是公开的,除非有一种方法可以通过接口或基类从其他程序集访问这些类。

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

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