What is an internal sealed class in C#?

本秂侑毒 提交于 2019-12-20 09:08:30

问题


I was looking through some C# code for extending language support in VS2010 (Ook example). I saw some classes called internal sealed class

What do these do? Would one use them?


回答1:


It is a class that:

  • internal: Can only be accessed from within the assembly it is defined (or friend assemblies).
  • sealed: Cannot be inherited.

Marking classes as internal is a way of preventing outside users of an assembly from using them. It's really a form of design encapsulation and IMHO it is good practice to mark types that are not part of the intended public API\object models as internal. In the long term this prevents users of your library from coupling themselves to types which you did not intend them to. This sort of unintended coupling harms your ability to change and evolve the way your libraries are implemented as you cannot change them without breaking your clients. Using internal helps to keep the public and usable surface area of a library down to what is intended.

Marking classes as sealed prevents these classes from being inherited. This is a pretty drastic design intent which is sometimes useful if a class is already so specialized that it is sensible that no other functionality should be added to it via inheritance either directly or via overriding its behaviour.

internal and sealed modify types in quite different ways, but they can be used together.

NB You have some further scoping control of internal as you can define a set of other assemblies as 'friends'. These friend assemblies may access your internal types. This can be useful for defining sets of co-operating assemblies such as production and test assemblies. It is often desirable that a test assembly can see all the types in the assembly it is testing.




回答2:


  • internal: A class which can only be accessed inside the same assembly.

    Assembly1.dll:

    namespace test {
        internal class InternalClass {
        }
    
        public class PublicClass { 
        }
    } 
    

    Assembly2.dll:

    using test;
    ...
    InternalClass c1; // Error
    PublicClass c2; // OK
    
  • sealed: A class which cannot be derived from

    sealed class SealedClass { ... }
    
    class ChildClass : SealedClass {} //ERROR
    



回答3:


Internal means the member is accessible to other types that are defined in the same assembly. A Sealed class is sort of the oppositie of abstract. It can be instantiated but cannot serve as a base class. The primary reason to seal a class is to prevent your users from fiddling around with it and breaking it. It’s also the case that sealing a class permits certain compiler optimizations that are not possible with non-sealed classes.




回答4:


An internal sealed class is one that is:

internal - Only accessible from within the same assembly
sealed - Cannot be subclassed

In other words, there's no way for you to use it directly.




回答5:


Internal means it can be used only in same assembly,

The internal keyword is an access modifier for types and type members. Internal types or members are accessible only within files in the same assembly

sealed that can't be inherited

A sealed class cannot be inherited. It is an error to use a sealed class as a base class. Use the sealed modifier in a class declaration to prevent inheritance of the class.



来源:https://stackoverflow.com/questions/4619185/what-is-an-internal-sealed-class-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!