在 class 中定义 public 枚举有什么好处?

What is the advantage of defining an public Enum inside a class?

我一直在寻找使用 class Microsoft.SqlServer.Management.Smo.StoredProcedure 中的函数 ScriptHeader。我找到了如何去做(期望那里有一个命名空间文件夹)。但我只是好奇地想了解枚举在 class 之类的内部定义的原因。这样做的好处是什么?

Server ServerConnect = new Server(new ServerConnection("database"));
StoredProcedure proc = ServerConnect.Databases["myDB"].StoredProcedures[0];
string header = proc.ScriptHeader(ScriptNameObjectBase.ScriptHeaderType.ScriptHeaderForCreateOrAlter);

class 定义是:

namespace Microsoft.SqlServer.Management.Smo {
  public class ScriptNameObjectBase : NamedSmoObject
  {
    protected internal ScriptNameObjectBase();

    public enum ScriptHeaderType
    {
        ScriptHeaderForAlter = 0,
        ScriptHeaderForCreate = 1,
        ScriptHeaderForCreateOrAlter = 2
    }
  }
}

I'm just curious to understand the reason why the enum is defined inside a class like it. What is the advantage of doing it that way?

据我所知,该选择没有特别引人注目的优点或缺点。而且,这是一个不寻常的选择。

Microsoft 的框架设计指南不鼓励基础 class 库提供者以这种方式嵌套 public 类型,因为它可能会造成混淆。

此选择向使用此 class 的开发人员强调枚举类型将与包含 class 的操作强相关,并且仅与 class 相关。即强调"don't be using this enum for your own purposes; it's really an implementation detail of the outer class".

我不清楚为什么这个 class 的作者——他们显然在微软工作——在这种情况下没有遵循微软的指导方针。我想你必须问他们才能确定。

通常将一种类型嵌套在另一种类型中的原因是使内部类型成为外部类型的 实际 实现细节。也就是说,将内部类型隐藏在外部 class 中以避免 "polluting" 外部 class 的命名空间具有开发人员永远不应该使用的类型。但在这些情况下,您会将内部类型标记为内部、受保护或私有。