C# - 命名空间内的类型声明

发布于 2024-08-23 06:35:36 字数 188 浏览 7 评论 0原文

在命名空间内而不是在类中声明类型的可能用途是什么。

例如:

namespace Test
{
    public delegate void Ispossible();
}

这是有效的&不会产生任何编译错误,但我不明白为什么我们会以这种方式声明它,而不是在类中声明它。

what could be a possible use of declaring types within a namespace but not in a class.

For ex:

namespace Test
{
    public delegate void Ispossible();
}

This is valid & does not generate any compilation errors but i can't think of why we would declare it this way as opposed to inside a class.

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

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

发布评论

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

评论(4

长不大的小祸害 2024-08-30 06:35:36

命名空间是 .NET 中的高级组织单元。

在类中声明类型通常是不受欢迎的(但是,与所有事情一样,这不是 100% 的规则),因为它会使类型更紧密地耦合并且更难以查找。

VB.NET 模块在某种程度上是一个例外(编辑:它们实际上更像是编译器技巧/语法糖),但通常 .NET 生态系统中的所有内容都包含在命名空间中。

您的示例适合重用;如果它在一个类中,则意味着委托只能由该类使用,并且可能会导致不必要地引入重复的委托。


更新:当仅使用少数类型时,命名空间似乎没有多大用处,但如果没有它们,任何规模的项目都将是一场组织灾难。想象一下没有命名空间的 .NET 框架,一项(可能早已过时)计数使该框架有 3500 种类型。

命名空间就像文档的文件夹或抽屉;几张松散的纸张很容易管理,但如果您有很多页,那么找到您需要的纸张就会变得很痛苦。

阅读一下文档,它很短而且不是很复杂(也不是命名空间),但有一些不错的要点 MSDN - 命名空间 (c#)

A namespace is a high-level unit of organization within .NET.

Declaring types within classes is typically frowned upon (but, as with all things, it's not a 100% rule) because it can make the types more tightly coupled and more difficult to find.

VB.NET Modules are somewhat of an exception (edit: they're really more of a compiler trick/syntactical-sugar), but normally everything in the .NET ecosystem is contained in a namespace.

Your example lends itself to reuse; if it were within a class then it would imply that delegate should only be used by that class and would likely lead to duplicate delegates needlessly being introduced.


Update: When working with only a handful of types namespaces don't seem of much use, but without them a project of any size would be an organizational catastrophe. Imagine the .NET framework without namespaces, one (probably long outdated) count puts the framework at 3500 Types.

Namespaces are like folders or drawers for documents; a few loose papers are easy to manage but if you have many pages then finding the one you need becomes painful.

Give the documentation a read, it's short and not terribly complicated (neither are namespaces) but has a couple decent points MSDN - Namespace (c#)

掀纱窥君容 2024-08-30 06:35:36

If it is a multi purpose delegate such as Func<TResult>, EventHandler which is not related to a particular class then you should declare it in the namespace directly.

日久见人心 2024-08-30 06:35:36

您的措辞(“在命名空间内而不是在类中声明类型的可能用途是什么。”)表明您在“类型”和“类”之间进行了区分。没有。类是一种类型。

那么,在什么条件下您希望直接在命名空间中声明一个类(即最常见的方式)?这些相同的原因也适用于其他类型。

Your phrasing ("what could be a possible use of declaring types within a namespace but not in a class."), indicates that you draw a distinction betweens "types" and "classes". There is none. A class is a type.

So, under what conditions would you want to declare a class directly in a namespace (i.e, the way it is most commonly done) ? These same reasons apply to other kinds of types.

森林迷了鹿 2024-08-30 06:35:36

命名空间和类都可以用来按层次结构组织信息。然而,命名空间允许跨 dll 边界分布定义,但类则不允许。类还要求将类名放在类型名之前,但命名空间允许使用关键字“using”。

因此,如果您想在不同 dll 的一个命名空间中定义委托,则可以使用命名空间。

如果您想强制人们在类型名称前添加任何其他名称而不能够依赖名称空间上下文,那么您可以使用类。

Both namespaces and classes can be used to organize information in hierarchy. However namespaces allow to distribute definition across dll boundaries, but classes do not. Also classes require to put class name before type name, but namespaces allow to use keyword "using".

So if you want to define delegates in one namespace in different dlls, you use namespaces.

If you want to force people to prefix type name with any other name without being able to rely on namespace context then you use classes.

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