What is an internal sealed class in C#?

后端 未结 6 456
长情又很酷
长情又很酷 2021-01-31 13:31

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?

6条回答
  •  不知归路
    2021-01-31 14:12

    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

    • Sealed: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/sealed
    • Internal: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/internal

提交回复
热议问题