命名空间与同名类名 [英] Namespace with same name as a class name

查看:450
本文介绍了命名空间与同名类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得以下情理之中的事,但它是不可能的,一个错误将被抛出:的命名空间'富'已经包含了'吧的定义。

I thought the following makes sense to do, but it's not possible and an error will be thrown: The namespace 'foo' already contains a definition for 'bar'.

namespace foo
{
    public class bar { ... }
}

namespace foo.bar
{
    public class baz : EventArgs { ... }
}

什么是命名空间的命名也是这样吗?

What would be the appropriate way of namespace naming for a case like this?

推荐答案

这真的取决于你想要达到的目的。如果你真的想巴兹的foo.bar命名空间下坐,因为它取决于/紧扣你吧可以使一个子类吧:

This really depends on what you are trying to achieve. If you really want baz to sit under the foo.bar namespace because it is dependent/closely linked you to bar can make it a child class of bar:

namespace foo
{
    public class bar 
    {
        public class baz : EventArgs { ... }
    }
}

您现在可以创建一个新的实例为:

You can now create a new instance as:

var test = new foo.bar.baz();

这篇关于命名空间与同名类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆