C# 中的内部密封类是什么?

发布于 2024-10-11 02:19:01 字数 100 浏览 3 评论 0原文

我正在查看一些 C# 代码来扩展 VS2010 中的语言支持(Ook 示例)。我看到一些名为内部密封类的类,

它们有什么作用?有人会使用它们吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我三岁 2024-10-18 02:19:01

它是一个具有以下特性的类:

  • internal:只能从它定义的程序集(或友元程序集)内部访问。
  • sealed:无法继承。

将类标记为内部是防止程序集的外部用户使用它们的一种方法。这实际上是一种设计封装形式,恕我直言,将不属于预期公共 API\对象模型的类型标记为内部是一种很好的做法。从长远来看,这可以防止您的库的用户将自己耦合到您不希望他们耦合的类型。这种意外的耦合会损害您更改和发展库实现方式的能力,因为您无法在不破坏客户端的情况下更改它们。使用internal有助于将图书馆的公共和可用表面积保持在预期范围内。

将类标记为sealed 可防止这些类被继承。这是一个相当激烈的设计意图,如果一个类已经如此专业化,以至于不应该通过直接继承或通过覆盖其行为向其添加其他功能是明智的,那么有时会很有用。

internalsealed 修改类型的方式截然不同,但它们可以一起使用。

注意您可以对内部进行进一步的范围控制,因为您可以将一组其他程序集定义为“友元”。这些友元程序集可能会访问您的内部类型。这对于定义协作组件集(例如生产和测试组件)非常有用。通常希望测试程序集能够看到它正在测试的程序集中的所有类型。

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.

臻嫒无言 2024-10-18 02:19:01
  • 内部:只能在同一程序集中访问的类。

    Assembly1.dll:

    命名空间测试{
        内部类InternalClass {
        }
    
        公共类公共类{ 
        }
    } 
    

    Assembly2.dll:

    使用测试;
    ...
    内部类c1; // 错误
    公共类c2; // 好的
    
  • 密封:无法派生的类

    密封类 SealedClass { ... }
    
    类 ChildClass : SealedClass {} //错误
    
  • 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
    
楠木可依 2024-10-18 02:19:01

内部意味着同一程序集中定义的其他类型可以访问该成员。密封类有点与抽象相反。它可以被实例化,但不能作为基类。密封类的主要原因是防止用户摆弄它并破坏它。密封类还允许某些编译器优化,而这对于非密封类是不可能的。

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.

且行且努力 2024-10-18 02:19:01

内部密封类是这样的类:

内部 - 只能从同一程序集中访问
sealed - 无法子类化

换句话说,您无法直接使用它。

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.

忆依然 2024-10-18 02:19:01

Internal 表示它只能在同一程序集中使用,

内部关键字是一个访问
类型和类型成员的修饰符。
内部类型或成员是
只能在以下文件中访问
相同的程序集

密封但不能被继承

密封类不能被继承。它
使用密封类作为错误是错误的
基类。在中使用密封修饰符
一个类声明来防止
类的继承。

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.

幽梦紫曦~ 2024-10-18 02:19:01

INTERNAL

内部类型或成员只能在同一程序集中的文件内访问。

示例

// 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

首先,让我们从定义开始; seal 是一个修饰符,如果应用于类,则使其不可继承;如果应用于虚拟方法或属性,则使其不可验证

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

其用法的一个示例是专用类/方法或属性,其中潜在的更改可能会使它们停止按预期工作(例如,System.Drawing 命名空间的 Pens 类)。

...
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; }
        ...
    }
}

因为密封类不能被继承,所以不能用作基类,因此抽象类不能使用sealed 修饰符。同样重要的是要提到结构是隐式密封的

示例

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 文档

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文