C#代码里没有命名空间“namespace ---{}”但也可以运行,为什么?

liuzhenpolestar 2010-08-08 11:19:07
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref10/html/P_System_Reflection_ParameterInfo_Attributes.htm
小弟今天在MS帮助文档上看到了下面一段代码,发现里面没有写命名空间,我把它粘贴运行后,一切正常。我不明白为什么没有命名空间程序也能正常运行?下面代码里的两个类又属于谁?若其他项目想要调用这两个类该从哪找?希望各位大侠给个解释,能让小弟拨开迷雾见太阳。谢谢。
using System;
using System.Reflection;
public class MyClass1
{
public int MyMethod(int i, out short j, ref long k)
{
j = 2;
return 0;
}
}

public class ParameterInfo_Attributes
{
public static void Main()
{
// Get the type.
Type myType = typeof(MyClass1);
// Get the method named 'MyMethod' from the type.
MethodBase myMethodBase = myType.GetMethod("MyMethod");
// Get the parameters associated with the method.
ParameterInfo[] myParameters = myMethodBase.GetParameters();
Console.WriteLine("\nThe method {0} has the {1} parameters :",
"ParameterInfo_Example.MyMethod", myParameters.Length);
// Print the attributes associated with each of the parameters.
for (int i = 0; i < myParameters.Length; i++)
Console.WriteLine("\tThe {0} parameter has the attribute : {1}",
i + 1, myParameters[i].Attributes);
Console.ReadKey();
}
}
...全文
2490 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
麦麦_同学 2010-08-10
  • 打赏
  • 举报
回复
命名空间是当你用到这个类时,命名空间才用的到,类不用可以不写命名空间
wuyq11 2010-08-08
  • 打赏
  • 举报
回复
其他项目调用就要生成DLL文件,命名命名空间
liuzhenpolestar 2010-08-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qldsrx 的回复:]
当然可以了,这就好比你把文件直接丢在C盘,而不是在C盘下面建个文件夹存放,命名空间的作用就相当于文件夹,归类用的。
[/Quote]
哦,这点我明白了,可是若其他项目调用时,该怎么办?
liuzhenpolestar 2010-08-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xiaowang1026 的回复:]
楼主可以通过反汇编查看这个类的详细信息的
[/Quote]
怎么通过反汇编查看这个类的信息??谢谢。
liuzhenpolestar 2010-08-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dancingbit 的回复:]
没有显式指定命名空间,那么它们从属于默认的命名空间,有时也称为全局命名空间。
[/Quote]
也就是说,若其他项目引用,用globe.ParameterInfo_Attributes,globe.MyClass1即可找到这两个类了??
HolyPlace 2010-08-08
  • 打赏
  • 举报
回复
没有使用自定义命名空间,这个默认空间有可能是system或者当前项目
dancingbit 2010-08-08
  • 打赏
  • 举报
回复
没有显式指定命名空间,那么它们从属于默认的命名空间,有时也称为全局命名空间。
空心兜兜 2010-08-08
  • 打赏
  • 举报
回复
让我想起了C语言直接
void main
afeng@2008 2010-08-08
  • 打赏
  • 举报
回复
楼主可以通过反汇编查看这个类的详细信息的
chsl01 2010-08-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qldsrx 的回复:]
当然可以了,这就好比你把文件直接丢在C盘,而不是在C盘下面建个文件夹存放,命名空间的作用就相当于文件夹,归类用的。
[/Quote]

学习
afeng@2008 2010-08-08
  • 打赏
  • 举报
回复
理论上默认他是继承 object 类,所以应该是system命名空间了
qldsrx 2010-08-08
  • 打赏
  • 举报
回复 1
当然可以了,这就好比你把文件直接丢在C盘,而不是在C盘下面建个文件夹存放,命名空间的作用就相当于文件夹,归类用的。

111,093

社区成员

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

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

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