{"id":3482,"date":"2025-04-06T23:34:41","date_gmt":"2025-04-06T15:34:41","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3482"},"modified":"2025-04-06T23:34:41","modified_gmt":"2025-04-06T15:34:41","slug":"c-%e5%8f%98%e9%87%8f%e4%bd%9c%e7%94%a8%e5%9f%9f%ef%bc%88scope-of-variables-in-c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/06\/c-%e5%8f%98%e9%87%8f%e4%bd%9c%e7%94%a8%e5%9f%9f%ef%bc%88scope-of-variables-in-c%ef%bc%89\/","title":{"rendered":"C# \u53d8\u91cf\u4f5c\u7528\u57df\uff08Scope of Variables in C#\uff09"},"content":{"rendered":"\n<p>\u672c\u6587\u5168\u9762\u3001\u8be6\u7ec6\u6574\u7406\u7684 <strong>C# \u53d8\u91cf\u4f5c\u7528\u57df\uff08Scope\uff09<\/strong> \u8bb2\u89e3\u5185\u5bb9\uff0c\u6db5\u76d6\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u3001\u53ef\u8bbf\u95ee\u8303\u56f4\u3001\u4f5c\u7528\u57df\u5206\u7c7b\u3001\u4ee3\u7801\u793a\u4f8b\uff0c\u4f9b\u6df1\u5165\u4e86\u89e3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u4f5c\u7528\u57df\uff1f<\/h2>\n\n\n\n<p>\u5728 C# \u4e2d\uff0c<strong>\u4f5c\u7528\u57df\uff08Scope\uff09<\/strong> \u6307\u7684\u662f<strong>\u53d8\u91cf\u53ef\u4ee5\u88ab\u8bbf\u95ee\u6216\u4f7f\u7528\u7684\u4ee3\u7801\u8303\u56f4<\/strong>\u3002<\/p>\n\n\n\n<p>\u53d8\u91cf\u53ea\u5728\u5b83\u58f0\u660e\u7684\u4f5c\u7528\u57df\u8303\u56f4\u5185\u6709\u6548\uff0c\u8d85\u51fa\u4f5c\u7528\u57df\u5c06\u65e0\u6cd5\u8bbf\u95ee\u8be5\u53d8\u91cf\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">C# \u4f5c\u7528\u57df\u5206\u7c7b\u603b\u89c8<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4f5c\u7528\u57df\u7c7b\u578b<\/th><th>\u63cf\u8ff0<\/th><th>\u751f\u547d\u5468\u671f<\/th><\/tr><\/thead><tbody><tr><td><strong>\u5c40\u90e8\u4f5c\u7528\u57df<\/strong><\/td><td>\u53d8\u91cf\u5728\u65b9\u6cd5\u3001\u6784\u9020\u51fd\u6570\u3001\u4ee3\u7801\u5757\uff08\u5982 if\/for\uff09\u4e2d\u5b9a\u4e49<\/td><td>\u5757\u6267\u884c\u5b8c\u6bd5\u540e\u53d8\u91cf\u88ab\u9500\u6bc1<\/td><\/tr><tr><td><strong>\u53c2\u6570\u4f5c\u7528\u57df<\/strong><\/td><td>\u65b9\u6cd5\u53c2\u6570\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf<\/td><td>\u65b9\u6cd5\u6267\u884c\u671f\u95f4<\/td><\/tr><tr><td><strong>\u5b57\u6bb5\u4f5c\u7528\u57df<\/strong><\/td><td>\u7c7b\u6216\u7ed3\u6784\u4f53\u4e2d\u7684\u5b57\u6bb5<\/td><td>\u5bf9\u8c61\u5b58\u5728\u671f\u95f4\uff08\u5b9e\u4f8b\u5b57\u6bb5\uff09\u6216\u7a0b\u5e8f\u671f\u95f4\uff08\u9759\u6001\u5b57\u6bb5\uff09<\/td><\/tr><tr><td><strong>\u5168\u5c40\u53d8\u91cf<\/strong><\/td><td>\u4e0d\u652f\u6301\u771f\u6b63\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7 <code>static<\/code> \u5b57\u6bb5\u6a21\u62df<\/td><td>\u7a0b\u5e8f\u751f\u547d\u5468\u671f<\/td><\/tr><tr><td><strong>\u5757\u7ea7\u4f5c\u7528\u57df<\/strong><\/td><td>\u5728\u82b1\u62ec\u53f7 <code>{}<\/code> \u4e2d\u5b9a\u4e49\uff0c\u5982 <code>if<\/code>\u3001<code>while<\/code>\u3001<code>for<\/code> \u7b49<\/td><td>\u4ee3\u7801\u5757\u6267\u884c\u65f6<\/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\">1\ufe0f\u20e3 \u5c40\u90e8\u53d8\u91cf\u4f5c\u7528\u57df\uff08Local Scope\uff09<\/h2>\n\n\n\n<p>\u5c40\u90e8\u53d8\u91cf\u5b9a\u4e49\u5728\u65b9\u6cd5\u6216\u4ee3\u7801\u5757\u4e2d\uff0c\u53ea\u80fd\u5728\u8be5\u5757\u4e2d\u8bbf\u95ee\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void PrintNumber()\n{\n    int number = 10;\n    Console.WriteLine(number); \/\/ \u2705 \u53ef\u8bbf\u95ee\n}\n\nConsole.WriteLine(number); \/\/ \u274c \u9519\u8bef\uff1anumber \u4e0d\u5728\u5f53\u524d\u4f5c\u7528\u57df<\/code><\/pre>\n\n\n\n<p>\ud83d\udd17 \u5b98\u65b9\u6587\u6863\uff1a<br>https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/local-variables<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 \u53c2\u6570\u53d8\u91cf\u4f5c\u7528\u57df\uff08Parameter Scope\uff09<\/h2>\n\n\n\n<p>\u51fd\u6570\u53c2\u6570\u5728\u6574\u4e2a\u65b9\u6cd5\u4f53\u4e2d\u90fd\u662f\u53ef\u89c1\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void Greet(string name)\n{\n    Console.WriteLine(\"Hello \" + name); \/\/ \u2705 \u53ef\u8bbf\u95ee\u53c2\u6570 name\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\">3\ufe0f\u20e3 \u6210\u5458\u53d8\u91cf\u4f5c\u7528\u57df\uff08Field Scope\uff09<\/h2>\n\n\n\n<p>\u5b57\u6bb5\uff08\u6210\u5458\u53d8\u91cf\uff09\u5728\u6574\u4e2a\u7c7b\u4e2d\u90fd\u53ef\u89c1\uff08\u53d6\u51b3\u4e8e\u8bbf\u95ee\u4fee\u9970\u7b26\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person\n{\n    private string name = \"Alice\"; \/\/ \u6210\u5458\u53d8\u91cf\n\n    public void ShowName()\n    {\n        Console.WriteLine(name); \/\/ \u2705 name \u53ef\u5728\u7c7b\u65b9\u6cd5\u4e2d\u8bbf\u95ee\n    }\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\">4\ufe0f\u20e3 \u9759\u6001\u53d8\u91cf\u4f5c\u7528\u57df\uff08Static Field Scope\uff09<\/h2>\n\n\n\n<p>\u9759\u6001\u53d8\u91cf\u5c5e\u4e8e\u7c7b\u800c\u4e0d\u662f\u7c7b\u7684\u5b9e\u4f8b\uff0c\u53ef\u5728\u4efb\u4f55\u8bbf\u95ee\u8be5\u7c7b\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\uff08\u524d\u63d0\u662f\u4f5c\u7528\u57df\u5141\u8bb8\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Counter\n{\n    public static int Count = 0;\n}\n\nclass Program\n{\n    static void Main()\n    {\n        Counter.Count++; \/\/ \u2705 \u53ef\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8bbf\u95ee\u9759\u6001\u53d8\u91cf\n    }\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udd17 \u9759\u6001\u6210\u5458\u6587\u6863\uff1a<br>https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/static-classes-and-static-class-members<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5\ufe0f\u20e3 \u5757\u7ea7\u4f5c\u7528\u57df\uff08Block Scope\uff09<\/h2>\n\n\n\n<p>\u4ee3\u7801\u5757 <code>{}<\/code> \u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u53ea\u5728\u8be5\u5757\u4e2d\u6709\u6548\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void Loop()\n{\n    for (int i = 0; i &lt; 5; i++)\n    {\n        Console.WriteLine(i); \/\/ \u2705 i \u5728 for \u5757\u5185\u6709\u6548\n    }\n\n    \/\/ Console.WriteLine(i); \/\/ \u274c i \u8d85\u51fa\u4f5c\u7528\u57df\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\">6\ufe0f\u20e3 \u5d4c\u5957\u4f5c\u7528\u57df\uff08Nested Scopes\uff09<\/h2>\n\n\n\n<p>\u5185\u90e8\u4f5c\u7528\u57df\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\uff0c\u4f46\u5916\u90e8\u4e0d\u80fd\u8bbf\u95ee\u5185\u90e8\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void Test()\n{\n    int outer = 100;\n\n    if (true)\n    {\n        int inner = 200;\n        Console.WriteLine(outer); \/\/ \u2705 \u53ef\u4ee5\u8bbf\u95ee outer\n    }\n\n    \/\/ Console.WriteLine(inner); \/\/ \u274c inner \u4e0d\u53ef\u8bbf\u95ee\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\">7\ufe0f\u20e3 \u5b9e\u4f8b vs \u9759\u6001\u4f5c\u7528\u57df<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Demo\n{\n    int instanceVar = 5;           \/\/ \u5b9e\u4f8b\u53d8\u91cf\n    static int staticVar = 10;     \/\/ \u9759\u6001\u53d8\u91cf\n\n    void Show()\n    {\n        Console.WriteLine(instanceVar);  \/\/ \u2705 OK\n        Console.WriteLine(staticVar);    \/\/ \u2705 OK\n    }\n\n    static void StaticShow()\n    {\n        \/\/ Console.WriteLine(instanceVar); \/\/ \u274c \u9759\u6001\u65b9\u6cd5\u4e0d\u80fd\u8bbf\u95ee\u5b9e\u4f8b\u6210\u5458\n        Console.WriteLine(staticVar);     \/\/ \u2705 OK\n    }\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\">\u4f5c\u7528\u57df\u6ce8\u610f\u4e8b\u9879<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53d8\u91cf<strong>\u4e0d\u80fd\u5728\u5176\u4f5c\u7528\u57df\u5916\u88ab\u8bbf\u95ee<\/strong><\/li>\n\n\n\n<li>\u4e0d\u5141\u8bb8\u5728\u540c\u4e00\u4f5c\u7528\u57df\u5185\u91cd\u590d\u58f0\u660e\u540c\u540d\u53d8\u91cf<\/li>\n\n\n\n<li><code>var<\/code> \u63a8\u65ad\u53d8\u91cf\u5fc5\u987b\u5728\u58f0\u660e\u65f6\u521d\u59cb\u5316\uff0c\u5426\u5219\u65e0\u6cd5\u786e\u5b9a\u4f5c\u7528\u57df\u7c7b\u578b<\/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\">\u6743\u5a01\u5b66\u4e60\u8d44\u6e90<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/local-variables\">\ud83d\udd17 Microsoft C# \u5c40\u90e8\u53d8\u91cf\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/passing-parameters\">\ud83d\udd17 Microsoft C# \u65b9\u6cd5\u53c2\u6570\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/fields\">\ud83d\udd17 Microsoft C# \u7c7b\u4e0e\u7ed3\u6784\u4f53\u5b57\u6bb5<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5168\u9762\u3001\u8be6\u7ec6\u6574\u7406\u7684 C# \u53d8\u91cf\u4f5c\u7528\u57df\uff08Scope\uff09 \u8bb2\u89e3\u5185\u5bb9\uff0c\u6db5\u76d6\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u3001\u53ef\u8bbf\u95ee\u8303\u56f4\u3001\u4f5c\u7528\u57df\u5206\u7c7b\u3001\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3483,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3482","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\/3482","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=3482"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3482\/revisions"}],"predecessor-version":[{"id":3484,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3482\/revisions\/3484"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3483"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}