【问题标题】:Is there an advantage to using nested namespaces instead of Partial Public NotInheritable Classes使用嵌套命名空间而不是 Partial Public NotInheritable Classes 是否有优势
【发布时间】:2012-10-04 17:59:02
【问题描述】:

似乎使用Partial NotInheritable Class 的优点是可以在其中声明/实现共享方法。

这是一个代码示例,可以帮助您形象化问题。

Namespace MyNamespace.Utility
    ' Cannot Declare Shared Function
    Public NotInheritable Class Document
        ' Can Declare Shared Function        
    End Class
End Namespace

这是反对

Namespace MyNamespace
    Partial Public NotInheritable Class Utility
        ' Can Declare Shared Function
        Partial Public NotInheritable Class Document
            ' Can Declare Shared Function
        End Class
    End Class
End Namespace

【问题讨论】:

  • 注意“集合”是从另一个组件导入的命名空间。它是我在整个项目中定义的 TypeDef 类/对象(数据类型)的 KeyValuePairs 的集合。
  • 看这个老问题,应该是移到另一个SO社区了。

标签: vb.net class namespaces shared


【解决方案1】:

实用程序应该是一个命名空间,因为它充当一个逻辑分组。 Microsoft .NET Framework Nested Types Guidelines

我不同意您的说法,即密封类的优点是您可以定义静态方法,任何类都可以这样做。

【讨论】:

  • 对,但比较的是命名空间的密封类。您引用的文章
  • 请原谅。您引用的文章似乎与密封类与命名空间的概念不符。因为变量和对象实例的声明不适用。嵌套对象也是逻辑分组。它们的用法没有实际区别,对吗? MyComponent.Utility.Document.GetLatestReportCollection() 无论 Utility 是类还是命名空间都可以访问。
  • 也许更好的问题是,使用嵌套命名空间是否比使用由公共共享方法组成的 NotInheritable 类有好处。
  • 对于嵌套类型,您必须声明包含类型才能访问它。那就是您的代码将始终是 Utility.Document.GetLatestReportCollection()。使用命名空间,您可以将其缩减为 Document.GetLatestReportCollection()。授予智能感知可以减少编写的代码,但对我来说似乎是额外的噪音。我仍然不清楚您为什么要在密封类和命名空间之间进行比较,您的第二个示例显然是嵌套类型,恰好使用密封类。
猜你喜欢
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
  • 1970-01-01
  • 2015-08-15
  • 2011-04-05
  • 1970-01-01
  • 2018-07-18
相关资源
最近更新 更多