11

In Scala, it is possible to have define a base class or trait(interface) sealed, so that the only classes which are allowed to extend that class must be placed in the same class.

This is a useful pattern when coding libraries, is there any equivalent in .NET?

4

1 回答 1

21

模拟它的唯一方法是在抽象类中有一个私有构造函数并提供嵌套类的实现。

例子

public abstract class Foo
{
  private Foo(int k) {}

  public class Bar : Foo
  {
     public Bar() : base(10) {}
  }
}
于 2013-07-10T07:07:07.997 回答