Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an internal sealed class in C#?

Tags:

c#

sealed

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?

like image 715
Byron Whitlock Avatar asked Jan 06 '11 19:01

Byron Whitlock


People also ask

What is a seal class in C?

A sealed class, in C#, is a class that cannot be inherited by any class but can be instantiated. The design intent of a sealed class is to indicate that the class is specialized and there is no need to extend it to provide any additional functionality through inheritance to override its behavior.

What is sealed in C?

A class can be sealed by using the sealed keyword. The keyword tells the compiler that the class is sealed, and therefore, cannot be extended.

What is sealed class give example?

One of the best uses of sealed classes is when you have a class with static members. The Pens class of the System. Drawing namespace is one of the examples of the sealed class. The Pens class has static members that represent the pens with standard colors.

When would you use a sealed class?

Sealed Classes allow us to fix type hierarchies and forbid developers from creating new subclasses. They are useful when we have a very strict inheritance hierarchy, with a specific set of possible subclasses and no others.


2 Answers

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.

like image 172
Tim Lloyd Avatar answered Sep 25 '22 04:09

Tim Lloyd


  • 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 
like image 31
HyLian Avatar answered Sep 21 '22 04:09

HyLian