你能解释一下c#中internal关键字的实际用法吗?
我知道内部修饰符限制了对当前程序集的访问,但何时以及在何种情况下应该使用它?
你能解释一下c#中internal关键字的实际用法吗?
我知道内部修饰符限制了对当前程序集的访问,但何时以及在何种情况下应该使用它?
当前回答
如果Bob需要BigImportantClass,那么Bob需要让拥有项目A的人注册,以保证BigImportantClass将被编写以满足他的需求,测试以确保它满足他的需求,被记录为满足他的需求,并且将设置一个过程以确保它永远不会被更改,从而不再满足他的需求。
如果一个类是内部的,那么它就不需要经过这个过程,这为项目a节省了预算,他们可以把这些钱花在其他事情上。
内在的意义并不在于它让鲍勃的生活变得困难。而是它允许您控制Project A在特性、生命周期、兼容性等方面做出的昂贵承诺。
其他回答
前几天,也许是一个星期,在一个我不记得的博客上看到了一个有趣的东西。基本上这不是我的功劳,但我认为它可能有一些有用的应用。
假设您想让另一个程序集看到一个抽象类,但不希望有人能够继承它。Sealed不能工作,因为它是抽象的,这个程序集中的其他类确实从它继承。Private将不起作用,因为您可能希望在另一个程序集中声明一个Parent类。
namespace Base.Assembly { public abstract class Parent { internal abstract void SomeMethod(); } //This works just fine since it's in the same assembly. public class ChildWithin : Parent { internal override void SomeMethod() { } } } namespace Another.Assembly { //Kaboom, because you can't override an internal method public class ChildOutside : Parent { } public class Test { //Just fine private Parent _parent; public Test() { //Still fine _parent = new ChildWithin(); } } }
如您所见,它有效地允许某人使用父类而不能够继承。
唯一一件事我曾经使用内部关键字是许可证检查代码在我的产品;-)
内部类使您能够限制程序集的API。这有很多好处,比如让你的API更容易理解。
此外,如果程序集中存在错误,修复程序引入破坏性更改的可能性就会降低。如果没有内部类,您将不得不假设更改任何类的公共成员都将是破坏性的更改。对于内部类,您可以假设修改它们的公共成员只会破坏程序集的内部API(以及InternalsVisibleTo属性中引用的任何程序集)。
我喜欢在类级和程序集级进行封装。有一些人不同意这一点,但很高兴知道功能是可用的。
我发现内部被过度使用了。您真的不应该只向某些类公开某些功能,而不向其他使用者公开。
在我看来,这打破了界面,打破了抽象。这并不是说永远不应该使用它,而是更好的解决方案是重构到不同的类,或者在可能的情况下以不同的方式使用。然而,这并不总是可能的。
The reasons it can cause issues is that another developer may be charged with building another class in the same assembly that yours is. Having internals lessens the clarity of the abstraction, and can cause problems if being misused. It would be the same issue as if you made it public. The other class that is being built by the other developer is still a consumer, just like any external class. Class abstraction and encapsulation isnt just for protection for/from external classes, but for any and all classes.
另一个问题是,许多开发人员会认为他们可能需要在程序集中的其他地方使用它,并将其标记为内部,即使他们当时并不需要它。另一个开发商可能会认为它就在那里。通常,在有明确的需要之前,您希望将其标记为私有。
但其中有些可能是主观的,我并不是说永远不应该使用它。只在需要的时候使用。
当你的类或方法不完全符合面向对象范式,它们做危险的事情,需要从你控制下的其他类和方法调用,而你不想让其他人使用它们时。
public class DangerousClass {
public void SafeMethod() { }
internal void UpdateGlobalStateInSomeBizarreWay() { }
}