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

liuzhenpolestar liuzhenpolestar 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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();
}
}
...全文

相关推荐

C# 命名空间(Namespace)相关知识总结

主要介绍了C# 命名空间(Namespace)的相关知识,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以参考下

weixin_38545923

命名空间的使用,以及为何不用using namespace std,内含可运行代码

本例主要解释了为什么不用using namespace std;,因为如果这么使用了,在其它地方如果用到了标准库中的标识符(那么多标识符,不能保证都记住),就会报错,如果程序工程很复杂,可能会查找冲突很麻烦,因此在实际使用中,使用 using命令(本例中两种方式,对于C++标准库命名空间std,在实际用到的标识符前面采用加std::的方式,对于自己命名空间myown1,2,则使用using myown1::B的方式)比使用using编译命令(using namespace std方式,本例中using namespace myown2也是用的这种方式,所以才报错)更安全

第八代火影

Namespace 命名空间

Namespace 命名空间Namespace 命名空间Namespace 命名空间Namespace 命名空间Namespace 命名空间

lihaotian817

C#使用命名空间NameSpace实例

摘要:C#源码,综合实战,NameSpace使用  C#使用命名空间NameSpace的一个基础实例,以命令提示符控制台的形式显示出演示结果,挺基础的C#例子,使用using指令引入命名空间NameSpace,实例化NameSpace中的类,调用类中的Myls方法,建立命名空间,自定义NameSpace中的类,输出字符串

weixin_38744375

c# 命名空间和程序集

使用类的全权名: System.Text.StringBuilder sb = new System.Text.StringBuilder(); 上面的写法很繁琐,使用using语句引入命名空间: using System.Text; StringBuilder sb = new StringBuilder(); 对于编译器来说,命名空间就是为一个类型附加一些分隔符号,使名称更有唯一性。 c#的using指令是可选的,完全可以用类型的完整名称代替,c# 的using指令时指示编译器为 每一个类型附加不同的前缀,确定匹配的完整名称。 CLR不知道命名空间的任何事,CLR知道类的完整名称来锁定具体

weixin_38562725
评论 12
收藏 已收藏
分享
返回顶部
liuzhenpolestar
C# 111,129 成员 642,540 内容