C# 我应该默认使用内部可见性还是公共可见性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/106941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 14:15:10  来源:igfitidea点击:

Should I use internal or public visibility by default?

提问by 1800 INFORMATION

I'm a pretty new C# and .Net developer. I recently created an MMC snapin using C# and was gratified by how easy it was to do, especially after hearing a lot of horror stories by some other developers in my organisation about how hard it is to do in C++.

我是一个相当新的 C# 和 .Net 开发人员。我最近使用 C# 创建了一个 MMC 管理单元,并且对它做起来如此简单感到欣慰,尤其是在我的组织中的其他一些开发人员听到很多关于用 C++ 做它是多么困难的恐怖故事之后。

I pretty much went through the whole project at some point and made every instance of the "public" keyword to "internal", except as required by the runtime in order to run the snapin. What is your feeling on this, should you generally make classes and methods public or internal?

我几乎在某个时候完成了整个项目,并将“public”关键字的每个实例都设置为“internal”,除非运行时需要以运行管理单元。您对此有何看法,您通常应该将类和方法设为公开还是内部?

采纳答案by Stephen Wrighton

I believe in blackboxes where possible. As a programmer, I want a well defined blackbox which I can easily drop into my systems, and have it work. I give it values, call the appropriate methods, and then get my results back out of it.

我尽可能相信黑匣子。作为一名程序员,我想要一个定义明确的黑匣子,我可以轻松地将其放入我的系统中并使其工作。我给它赋值,调用适当的方法,然后从中取出我的结果。

To that end, give me only the functionality that the class needs to expose to work.

为此,只给我类需要公开工作的功能。

Consider an elevator. To get it to go to a floor, I push a button. That's the public interface to the black box which activates all the functions needed to get the elevator to the desired floor.

考虑电梯。为了让它上一层楼,我按下了一个按钮。这是黑匣子的公共接口,它激活将电梯带到所需楼层所需的所有功能。

回答by Heath Borders

It depends on how much control you have over code that consumes it. In my Java development, I make all my stuff public final by default because getters are annoying. However, I also have the luxury of being able to change anything in my codebase whenever I want. In the past, when I've had to release code to consumers, I've always used private variables and getters.

这取决于您对使用它的代码有多少控制。在我的 Java 开发中,我默认将我所有的东西都设为 public final,因为 getter 很烦人。但是,我也可以随时更改代码库中的任何内容。过去,当我不得不向消费者发布代码时,我总是使用私有变量和 getter。

回答by ColinD

You should tend toward exposing as little as possible to other classes, and think carefully about about what you do expose and why.

您应该倾向于尽可能少地向其他类公开,并仔细考虑您公开的内容以及原因。

回答by Ryan Farley

Do not choose a "default" pick what best fits the visibility needs for that particular class. When you choose a new class in Visual Studio, the template is created as:

不要选择“默认”选择最适合该特定类的可见性需求的内容。在 Visual Studio 中选择新类时,模板将创建为:

class Class1
{
}

Which is private (since no scope is specified). It is up to you to specify scope for the class (or leave as private). There should be a reason to expose the class.

这是私有的(因为没有指定范围)。由您来指定类的范围(或保留为私有)。应该有理由公开课程。

回答by Matt Blaine

I like to expose things as little as possible. Private, protected, internal, public: give classes, variables, properties, and functions the least amount of visibility they need for everything to still work.

我喜欢尽可能少地暴露事物。私有的、受保护的、内部的、公共的:为类、变量、属性和函数提供最少的可见性,让它们仍然可以工作。

I'll bump something's visibility up that chain toward public only when there's a good reason to.

只有在有充分理由的情况下,我才会将某些东西的可见性从这条链上推向公众。

回答by Jay Bazuzi

I prefer to avoid marking classes as publicunless I explicitly want my customer to consume them, and I am prepared to support them.

我宁愿避免将类标记为public除非我明确希望我的客户使用它们,并且我准备支持它们。

Instead of marking a class as internal, I leave the accessibility blank. This way, publicstands out to the eye as something notable. (The exception, of course, is nested classes, which have to be marked if they are to be visible even in the same assembly.)

internal我没有将类标记为,而是将可访问性留空。通过这种方式,public引人注目的东西引人注目。(当然,嵌套类是个例外,如果它们要在同一个程序集中可见,就必须标记它们。)

回答by Brownie

I think you should err on the side of internal classes and members. You can always increase an item's visibility but decreasing it can cause problems. This is especially true if you are building a framework for others.

我认为你应该在内部类和成员方面犯错。您始终可以增加项目的可见性,但降低它可能会导致问题。如果您正在为他人构建框架,则尤其如此。

You do need to be careful though not to hide useful functionality from your users. There are many useful methods in the .NET BCL that cannot be used without resorting to reflection. However by hiding these methods the surface area of what has to be tested and maintained is reduced.

您确实需要小心,但不要向用户隐藏有用的功能。.NET BCL 中有许多有用的方法,必须借助反射才能使用。然而,通过隐藏这些方法,减少了必须测试和维护的表面积。

回答by Ash

Is there any reason you need to use Internal instead of Private? You do realise that Internal has assembly level scope. In other words Internal classes/members are accessible to all classes in a multi-class assembly.

有什么理由需要使用 Internal 而不是 Private?您确实意识到 Internal 具有程序集级别范围。换句话说,多类程序集中的所有类都可以访问内部类/成员。

As some other answers have said, in general go for the highest level of encapsulation as possible (ie private) unless you actually need internal/protected/public.

正如其他一些答案所说,除非您确实需要内部/受保护/公共,否则通常尽可能使用最高级别的封装(即私有)。

回答by Ryan Lundy

What you did is exactly what you should do; give your classes the most minimal visibility you can. Heck, if you want to really go whole hog, you can make everythinginternal(at most) and use the InternalsVisibleToattribute, so that you can separate your functionality but still not expose it to the unknown outside world.

你所做的正是你应该做的;为您的课程提供尽可能少的可见性。哎呀,如果你真的想全力以赴,你可以制作一切internal(最多)并使用InternalsVisibleTo属性,这样你就可以分离你的功能,但仍然不会将它暴露给未知的外部世界。

The only reason to make things public is that you're packaging your project in several DLLs and/or EXEs and (for whatever reason) you don't care to use InternalsVisibleTo, or you're creating a library for use by third parties. But even in a library for use by third-parties, you should try to reduce the "surface area" wherever possible; the more classes you have available, the more confusing your library will be.

将事情公开的唯一原因是您将项目打包在多个 DLL 和/或 EXE 中并且(无论出于何种原因)您不想使用InternalsVisibleTo,或者您正在创建一个供第三方使用的库。但即使在供第三方使用的库中,您也应尽可能减少“表面积”;您可用的类越多,您的库就越混乱。

In C#, one good way to ensure you're using the minimum visibility possible is to leave off the visibility modifiers until you need them. Everything in C# defaults to the least visibility possible: internal for classes, and private for class members and inner classes.

在 C# 中,确保您使用尽可能低的可见性的一种好方法是在需要它们之前不要使用可见性修饰符。C# 中的所有内容都默认为尽可能低的可见性:内部用于类,私有用于类成员和内部类。

回答by Mike

I found a problem using internal classesas much as possible. You cannot have methods, properties, fields, etc of that type (or parameter type or return type) more visible than internal. This leads to have constructors that are internal, as well as properties. This shouldn't be a problem, but as a matter of fact, when using Visual Studio and the xaml designer, there are problems. False positive errors are detected by the designerdue to the fact that the methods are not public, user control properties seems not visible to the designer. I don't know if others have already fallen on such issues...

我发现尽可能多地使用内部类问题。您不能让该类型(或参数类型或返回类型)的方法、属性、字段等比内部更可见。这导致具有内部构造函数以及属性。这应该不是问题,但事实上,在使用 Visual Studio 和 xaml 设计器时,会出现问题。由于方法不是公开的,设计人员似乎看不到用户控件属性,设计人员会检测到误报错误。不知道其他人有没有遇到过这样的问题...