问题描述
任何人都可以告诉我,一个类与它所在的
命名空间同名是否合法。我做了,并且编译器没有给出错误,但是它引起了一些非常奇怪的名字解决问题。
-
Dave
Can anyone tell me, is it legal for a class to have the same name as the
namespace it is in. I did it, and the compiler didn''t give an error, but it
caused some very odd name resolving problems.
--
Dave
推荐答案
它是完全合法的你在答复中说:-)你看到了什么问题,应该没有。
it is perfectly legal as you said in your reply :-) What issues are you
seeing, there should not be any.
例如,以下内容不会编译为class UseMyTest抱怨
''MyTest''表示''命名空间'',其中''类''是预期的。
使用系统;
名称空间MyTest
{
公共类MyTest
{
public MyTest()
{
}
}
}
使用系统;
使用MyTest;
nam espace AnotherNamespace
{
公共类UseMyTest
{
public UseMyTest()
{
MyTest myTest = new MyTest();
}
}
}
For example, the following will not compile as the class UseMyTest complains
that
''MyTest'' denotes a ''namespace'' where a ''class'' was expected.
using System;
namespace MyTest
{
public class MyTest
{
public MyTest()
{
}
}
}
using System;
using MyTest;
namespace AnotherNamespace
{
public class UseMyTest
{
public UseMyTest()
{
MyTest myTest = new MyTest();
}
}
}
这篇关于C#中的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!