using System;
using System.Collections.Generic;
using System.Text;
using FirstSpace;
using SecondSpace;
using SomeNameSpace;
using SomeNameSpace.Nested;
namespace FirstSpace
{
internal class NamespaceCl
{
public string Func()
{
return "Inside first_space";
}
}
internal class AlphaWorker
{
public string Func()
{
return "Inside first_space";
}
}
}
namespace SecondSpace
{
internal class NamespaceCl
{
public string Func()
{
return "Inside second_space";
}
}
internal class BetaWorker
{
public string Func()
{
return "Inside second_space";
}
}
}
namespace SomeNameSpace
{
internal class OuterClass
{
public static string Identify()
{
return "In SomeNameSpace";
}
}
namespace Nested
{
internal class NestedNameSpaceClass
{
public static string SayHello()
{
return "In Nested";
}
}
}
}
namespace CSharpNamespaceLab
{
internal class Program
{
private static string Esc(string s)
{
if (s == null) return "null";
StringBuilder sb = new StringBuilder("\"");
foreach (char c in s)
{
switch (c)
{
case '\\': sb.Append("\\\\"); break;
case '"': sb.Append("\\\""); break;
case '\n': sb.Append("\\n"); break;
case '\r': sb.Append("\\r"); break;
case '\t': sb.Append("\\t"); break;
default:
if (c < 32) sb.AppendFormat("\\u{0:x4}", (int)c);
else sb.Append(c);
break;
}
}
sb.Append("\"");
return sb.ToString();
}
private static string CaseJson(string name, string summary, string expected, string actual)
{
bool passed = string.Equals(expected, actual, StringComparison.Ordinal);
return string.Format(
"{{\"name\":{0},\"summary\":{1},\"expected\":{2},\"actual\":{3},\"passed\":{4}}}",
Esc(name), Esc(summary), Esc(expected), Esc(actual), passed ? "true" : "false"
);
}
private static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
List<string> cases = new List<string>();
// Case 1: Two namespaces with identical class names accessed by qualified names
FirstSpace.NamespaceCl fc = new FirstSpace.NamespaceCl();
SecondSpace.NamespaceCl sc = new SecondSpace.NamespaceCl();
string actual1 = string.Format(
"{0}|{1}|distinct={2}",
fc.Func(), sc.Func(), fc.GetType() != sc.GetType()
);
cases.Add(CaseJson(
"namespace_isolation_and_qualified_access",
"验证不同命名空间下的同名类互不冲突并通过完全限定名区分实例化",
"Inside first_space|Inside second_space|distinct=True",
actual1
));
// Case 2: using directive omits namespace prefix
AlphaWorker a = new AlphaWorker();
BetaWorker b = new BetaWorker();
bool sameConsoleType = typeof(Console) == typeof(System.Console);
string actual2 = string.Format(
"{0}|{1}|consoleEq={2}",
a.Func(), b.Func(), sameConsoleType
);
cases.Add(CaseJson(
"using_directive_resolution",
"验证通过 using 指令省略命名空间前缀访问不同命名空间的无冲突类型与 System.Console",
"Inside first_space|Inside second_space|consoleEq=True",
actual2
));
// Case 3: Nested namespace access via dot operator and using
string outerMsg = OuterClass.Identify();
string nestedQualified = SomeNameSpace.Nested.NestedNameSpaceClass.SayHello();
string nestedDirect = NestedNameSpaceClass.SayHello();
string actual3 = string.Format(
"{0}|{1}|{2}|ns={3}",
outerMsg, nestedQualified, nestedDirect, typeof(NestedNameSpaceClass).Namespace
);
cases.Add(CaseJson(
"nested_namespace_access",
"验证嵌套命名空间的定义、点运算符限定访问与 using 导入直接访问",
"In SomeNameSpace|In Nested|In Nested|ns=SomeNameSpace.Nested",
actual3
));
Console.WriteLine(string.Format(
"{{\"version\":{0},\"cases\":[{1}]}}",
Esc("CLR " + Environment.Version.ToString()),
string.Join(",", cases.ToArray())
));
}
}
}
本例需在 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。
入口文件:projects/csharp-namespace-lab/ExploreNamespace.cs
本地运行步骤快速上手
- 点击上方“保存源码”保存为
ExploreNamespace.cs,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联42章) · 含当前章节或实例 · mbskycom202612a1321.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1343.zip