{"id":3532,"date":"2025-04-13T13:58:48","date_gmt":"2025-04-13T05:58:48","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3532"},"modified":"2025-04-13T13:58:48","modified_gmt":"2025-04-13T05:58:48","slug":"c-%e5%91%bd%e5%90%8d%e7%a9%ba%e9%97%b4%ef%bc%88namespace%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/13\/c-%e5%91%bd%e5%90%8d%e7%a9%ba%e9%97%b4%ef%bc%88namespace%ef%bc%89\/","title":{"rendered":"C# \u547d\u540d\u7a7a\u95f4\uff08Namespace\uff09"},"content":{"rendered":"\n<p>\u672c\u7ae0\u8282\u662f\u5173\u4e8e <strong>C# \u547d\u540d\u7a7a\u95f4\uff08Namespace\uff09<\/strong> \u7684\u5168\u9762\u8be6\u7ec6\u4e13\u4e1a\u89e3\u8bfb\uff0c\u5305\u62ec\u8bed\u6cd5\u3001\u7528\u9014\u3001\u89c4\u5219\u3001\u793a\u4f8b\u3001\u547d\u540d\u89c4\u8303\u3001\u7ec4\u7ec7\u7ed3\u6784\u3001\u76f8\u5173\u6280\u672f\u5bf9\u6bd4\u3001\u6700\u4f73\u5b9e\u8df5\u7b49\uff0c\u5e76\u9644\u4e0a\u6743\u5a01\u51fa\u5904\u94fe\u63a5\uff0c\u9002\u5408\u5f00\u53d1\u5de5\u7a0b\u5e08\u6df1\u5ea6\u5b66\u4e60\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u4ec0\u4e48\u662f\u547d\u540d\u7a7a\u95f4\uff1f<\/h2>\n\n\n\n<p><strong>\u547d\u540d\u7a7a\u95f4\uff08<code>namespace<\/code>\uff09<\/strong> \u662f\u7528\u4e8e\u5c06\u4e00\u7ec4\u76f8\u5173\u7684 <strong>\u7c7b\u3001\u63a5\u53e3\u3001\u7ed3\u6784\u3001\u679a\u4e3e\u3001\u59d4\u6258\u7b49\u7c7b\u578b<\/strong> \u7ec4\u7ec7\u5728\u4e00\u8d77\u7684\u903b\u8f91\u5206\u7ec4\u65b9\u5f0f\uff0c\u76ee\u7684\u662f\u4e3a\u4e86 <strong>\u907f\u514d\u547d\u540d\u51b2\u7a81<\/strong> \u5e76\u5b9e\u73b0\u66f4\u6e05\u6670\u7684\u5c42\u7ea7\u7ed3\u6784\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83c\udfaf \u7c7b\u4f3c Java \u5305\uff08<code>package<\/code>\uff09\u3001Python \u6a21\u5757\uff08<code>module<\/code>\uff09<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u547d\u540d\u7a7a\u95f4\u7684\u57fa\u672c\u8bed\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace Company.Product.Module {\n    public class MyClass {\n        public void Print() =&gt; Console.WriteLine(\"Hello from MyClass\");\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4f7f\u7528\u65f6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using Company.Product.Module;\n\nMyClass obj = new MyClass();\nobj.Print();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u4f7f\u7528 <code>using<\/code> \u5f15\u5165\u547d\u540d\u7a7a\u95f4<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;<\/code><\/pre>\n\n\n\n<p>\u2705 \u7b80\u5316\u8bbf\u95ee\uff0c\u4e0d\u9700\u5168\u9650\u5b9a\u540d<br>\u2757 \u6ce8\u610f\u907f\u514d\u540d\u79f0\u51b2\u7a81<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u547d\u540d\u7a7a\u95f4\u7684\u5d4c\u5957\u4e0e\u6587\u4ef6\u7ed3\u6784<\/h2>\n\n\n\n<p>\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u5d4c\u5957\u5728\u591a\u4e2a\u7ea7\u522b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace Outer {\n    namespace Inner {\n        class NestedClass { }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u7b49\u540c\u4e8e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace Outer.Inner {\n    class NestedClass { }\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udd38 \u901a\u5e38\u6587\u4ef6\u5939\u7ed3\u6784\u4e5f\u4f1a\u5bf9\u5e94\u547d\u540d\u7a7a\u95f4\u5c42\u6b21\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u547d\u540d\u7a7a\u95f4\u522b\u540d\uff08Alias\uff09<\/h2>\n\n\n\n<p>\u9002\u7528\u4e8e\u89e3\u51b3\u547d\u540d\u51b2\u7a81\u6216\u7b80\u5316\u8bbf\u95ee\u8def\u5f84\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using ProjA = ProjectAlpha.Namespace;\nusing ProjB = ProjectBeta.Namespace;<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ProjA.MyClass a = new ProjA.MyClass();\nProjB.MyClass b = new ProjB.MyClass();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. .NET Framework \u5e38\u7528\u547d\u540d\u7a7a\u95f4<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u547d\u540d\u7a7a\u95f4<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td><code>System<\/code><\/td><td>\u6240\u6709\u57fa\u672c\u7c7b\u578b\u3001\u57fa\u7840\u7c7b\u5e93<\/td><\/tr><tr><td><code>System.Collections<\/code><\/td><td>\u975e\u6cdb\u578b\u96c6\u5408<\/td><\/tr><tr><td><code>System.Collections.Generic<\/code><\/td><td>\u6cdb\u578b\u96c6\u5408\uff08\u5982 <code>List&lt;T&gt;<\/code>\uff09<\/td><\/tr><tr><td><code>System.IO<\/code><\/td><td>\u6587\u4ef6\u3001\u6d41<\/td><\/tr><tr><td><code>System.Net<\/code><\/td><td>\u7f51\u7edc\u7f16\u7a0b<\/td><\/tr><tr><td><code>System.Linq<\/code><\/td><td>LINQ \u67e5\u8be2\u64cd\u4f5c<\/td><\/tr><tr><td><code>System.Threading<\/code><\/td><td>\u591a\u7ebf\u7a0b\u652f\u6301<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. C# \u6587\u4ef6\u9876\u90e8\u7684 <code>using<\/code> \u4e0e\u547d\u540d\u7a7a\u95f4\u58f0\u660e\u533a\u522b<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>using<\/code>\uff1a\u7528\u4e8e\u5bfc\u5165\u5176\u4ed6\u547d\u540d\u7a7a\u95f4\u3002<\/li>\n\n\n\n<li><code>namespace<\/code>\uff1a\u7528\u4e8e\u5b9a\u4e49\u5f53\u524d\u4ee3\u7801\u6240\u5c5e\u547d\u540d\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\nnamespace MyProject.Utilities {\n    class Helper { }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. \u547d\u540d\u7a7a\u95f4\u8bbe\u8ba1\u7684\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5efa\u8bae<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u2705 \u9075\u5faa PascalCase<\/td><td>\u5982 <code>MyCompany.MyApp.Services<\/code><\/td><\/tr><tr><td>\u2705 \u4e00\u81f4\u6027<\/td><td>\u547d\u540d\u7a7a\u95f4\u5e94\u53cd\u6620\u7269\u7406\u76ee\u5f55\u7ed3\u6784<\/td><\/tr><tr><td>\u2705 \u907f\u514d\u8fc7\u6df1\u5d4c\u5957<\/td><td>\u4e0d\u5efa\u8bae\u8d85\u8fc7 3-4 \u5c42<\/td><\/tr><tr><td>\u2705 \u62c6\u5206\u6a21\u5757<\/td><td>\u4e0d\u540c\u6a21\u5757\u3001\u5c42\uff08\u5982 Data, Service, UI\uff09\u4f7f\u7528\u4e0d\u540c\u547d\u540d\u7a7a\u95f4<\/td><\/tr><tr><td>\u2705 \u63a5\u53e3\u3001\u5b9e\u73b0\u5206\u5f00<\/td><td>\u6bd4\u5982 <code>MyApp.Services.Interfaces<\/code> vs <code>MyApp.Services.Impl<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. \u547d\u540d\u51b2\u7a81\u7684\u89e3\u51b3<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Text;\nusing MyApp.Text;  \/\/ \u4e5f\u6709 Text \u547d\u540d\u7a7a\u95f4\n\n\/\/ \u6307\u5b9a\u5b8c\u5168\u9650\u5b9a\u540d\nSystem.Text.StringBuilder sb = new System.Text.StringBuilder();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. \u5b98\u65b9\u6743\u5a01\u6587\u6863\u4e0e\u53c2\u8003\u94fe\u63a5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udd17 <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/namespaces\/\">Microsoft Learn: C# Namespaces<\/a><\/li>\n\n\n\n<li>\ud83d\udd17 <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/?view=net-8.0\">.NET API Browser &#8211; Namespaces<\/a><\/li>\n\n\n\n<li>\ud83d\udd17 <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/keywords\/namespace\">C# Language Reference &#8211; namespace<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">11. \u5b9e\u9645\u9879\u76ee\u7ed3\u6784\u793a\u610f\uff08.NET \u793a\u4f8b\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>MyApp\/\n\u251c\u2500\u2500 Controllers\/           =&gt; MyApp.Controllers\n\u251c\u2500\u2500 Services\/              =&gt; MyApp.Services\n\u2502   \u251c\u2500\u2500 Interfaces\/        =&gt; MyApp.Services.Interfaces\n\u2502   \u2514\u2500\u2500 Implementations\/   =&gt; MyApp.Services.Implementations\n\u251c\u2500\u2500 Models\/                =&gt; MyApp.Models\n\u2514\u2500\u2500 Utilities\/             =&gt; MyApp.Utilities<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\uff0c\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7ae0\u8282\u662f\u5173\u4e8e C# \u547d\u540d\u7a7a\u95f4\uff08Namespace\uff09 \u7684\u5168\u9762\u8be6\u7ec6\u4e13\u4e1a\u89e3\u8bfb\uff0c\u5305\u62ec\u8bed\u6cd5\u3001\u7528\u9014\u3001\u89c4\u5219\u3001\u793a\u4f8b\u3001\u547d\u540d\u89c4\u8303\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3533,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3532","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3532"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3532\/revisions"}],"predecessor-version":[{"id":3534,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3532\/revisions\/3534"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3533"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}