75

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?

3
  • 11
    This would be a more interesting question if you had asked "why seal it if it is already internal?" Jan 6, 2011 at 20:12
  • @Hans Passant Maybe to stop other developers extending it in maintenance or other future development? (Trivial to remove of course but if the class wouldn't work well with much inheritance it makes sense to mark as such by sealing it
    – Kurru
    Jan 6, 2011 at 21:00
  • @Hans Passant - why not to allow for inheritance within the given assembly only? Plenty of valid use scenarios for an internal class that can be extended out into subclasses.
    – dexter
    Jan 6, 2011 at 21:01

6 Answers 6

159

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.

5
  • 7
    Some would argue that sealed isn't really "drastic" and should be considered the default... I've seen Eric Lippert and Jon Skeet talk about this.
    – Casey
    Apr 4, 2014 at 15:34
  • 1
    @Casey I totally disagree. I even believe the explicit need for virtual is drastic in c# and one of the big disadvantages (maybe the only big one) and the default behavior should be to always allow extensibility unless explicitly prohibited. It is far more common to allow overriding behavior than it is to prohibit it.
    – Arijoon
    Apr 30, 2016 at 11:54
  • @Arijoon The risk is that someone overriding your method can totally break your assumptions. For example, imagine if virtual was the default and I overrode your InitializeObject method. Well, who's to say it would still work right?
    – Casey
    May 1, 2016 at 0:52
  • 2
    @Casey, I can totally see your side and benefit of that. Nevertheless isn't it better to set InitializeObject as sealed If I don't want to allow inheritance on it?
    – Arijoon
    May 1, 2016 at 9:25
  • 1
    @Arijoon I don't know; in my opinion the language should make it easier to write code that will function as expected than code that may be too brittle.
    – Casey
    May 2, 2016 at 14:32
20
  • 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
    
13

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.

6

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.

4

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.

2

INTERNAL

Internal types or members are accessible only within files in the same assembly.

Example

// Assembly1.cs  
// Compile with: /target:library  
internal class BaseClass   
{  
   public static int intM = 0;  
} 
// Assembly1_a.cs  
// Compile with: /reference:Assembly1.dll  
class TestAccess   
{  
   static void Main()   
   {  
      var myBase = new BaseClass();   // compile error 
   }  
} 

SEALED

First of all, let's start with a definition; sealed is a modifier which if applied to a class make it non-inheritable and if applied to virtual methods or properties makes them non-ovveridable.

public sealed class A { ... }
public class B 
{
    ...
    public sealed string Property { get; set; }
    public sealed void Method() { ... }
}

An example of its usage is specialized class/method or property in which potential alterations can make them stop working as expected (for example, the Pens class of the System.Drawing namespace).

...
namespace System.Drawing
{
    //
    // Summary:
    //     Pens for all the standard colors. This class cannot be inherited.
    public sealed class Pens
    {
        public static Pen Transparent { get; }
        public static Pen Orchid { get; }
        public static Pen OrangeRed { get; }
        ...
    }
}

Because a sealed class cannot be inherited, it cannot be used as base class and by consequence, an abstract class cannot use the sealed modifier. It's also important to mention that structs are implicitly sealed.

Example

public class BaseClass {
    public virtual string ShowMessage()
    {
        return "Hello world";
    }
    public virtual int MathematicalOperation(int x, int y)
    {
        return x + y;
    }
}
public class DerivedClass : BaseClass {
    public override int MathematicalOperation(int x, int y) 
    {
        // since BaseClass has a method marked as virtual, DerivedClass can override it's behavior
        return x - y;
    }
    public override sealed string ShowMessage()
    {
        // since BaseClass has a method marked as virtual, DerivedClass can override it's behavior but because it's sealed prevent classes that derive from it to override the method
        return "Hello world sealed";
    }
}
public class DerivedDerivedClass : DerivedClass
{
    public override int MathematicalOperation(int x, int y)
    {
        // since BaseClass has a method marked as virtual, DerivedClass can override it's behavior
        return x * y;
    }
    public override  string ShowMessage() { ... } // compile error
}
public sealed class SealedClass: BaseClass {
    public override int MathematicalOperation(int x, int y)
    {
        // since BaseClass has a method marked as virtual, DerivedClass can override it's behavior
        return x * y;
    }
    public override string ShowMessage()
    {
        // since BaseClass has a method marked as virtual, DerivedClass can override it's behavior but because it's sealed prevent classes that derive from it to override the method
        return "Hello world";
    }
}
public class DerivedSealedClass : SealedClass
{
    // compile error
}

Microsoft documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.