一句话问题,近来看看吧。

DeadlyRemind 2009-04-02 10:18:17

using System;
namespace xxx{}
//-----------------------

namespace xxx
{
using System;
}


应用在命名空间里面,和外面区别何在? 为什么部分论者推荐放在命名空间里面呢?
...全文
68 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
DeadlyRemind 2009-04-02
  • 打赏
  • 举报
回复
up一下,似乎分数给的有点少,不够热闹哇。
wartim 2009-04-02
  • 打赏
  • 举报
回复
你新建一个winform项目名称填ABC.System.dll然后确定看看
设计器都打不开

设计器无法处理第 32 行的代码: this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 方法“InitializeComponent”内的代码由设计器生成,不应手动修改。请移除任何更改,然后尝试重新打开设计器。
隐藏 编辑
tocoya 2009-04-02
  • 打赏
  • 举报
回复
真有思想!谢谢楼主!
cppfaq 2009-04-02
  • 打赏
  • 举报
回复
There is actually a (subtle) difference between the two. Imagine you have the following code in File1.cs:

// File1.cs
using System;
namespace Outer.Inner{ class Foo { static void Bar() { double d = Math.PI;
} }}


//Now imagine that someone adds another file (File2.cs) to the project that looks like this:


 // File2.cs
namespace Outer{ class Math { }}

The compiler searches Outer before looking at those using statements
outside the namespace, so it finds Outer.Math instead of System.Math. Unfortunately (or perhaps fortunately?),
Outer.Math has no PI member, so File1 is now broken.

This changes if you put the using inside your namespace declaration, as follows:

// File1b.cs
namespace Outer.Inner{ using System; class Foo { static void Bar() {
double d = Math.PI; } }}
Now the compiler searches System before searching Outer, finds System.Math,
and all is well.

Some would argue that Math might be a bad name for a user-defined class, since there's already one in System;
the point here is just that there is a difference, and it affects the maintainability of your code.

It's also interesting to note what happens if Foo is in namespace Outer, rather than Outer.Inner. In that case,
adding Outer.Math in File2 breaks File1 regardless of where the using goes. This implies that the compiler
searches the innermost enclosing namespace before it looks at any using statements.
Mr-Jee 2009-04-02
  • 打赏
  • 举报
回复
为了防止类名冲突

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧