{"id":3470,"date":"2025-04-06T23:24:55","date_gmt":"2025-04-06T15:24:55","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3470"},"modified":"2025-04-06T23:24:55","modified_gmt":"2025-04-06T15:24:55","slug":"c-%e5%9f%ba%e6%9c%ac%e8%af%ad%e6%b3%95%e8%af%a6%e8%a7%a3%ef%bc%88c-basic-syntax%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/06\/c-%e5%9f%ba%e6%9c%ac%e8%af%ad%e6%b3%95%e8%af%a6%e8%a7%a3%ef%bc%88c-basic-syntax%ef%bc%89\/","title":{"rendered":"C# \u57fa\u672c\u8bed\u6cd5\u8be6\u89e3\uff08C# Basic Syntax\uff09"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u76ee\u5f55\u7ed3\u6784<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>C# \u57fa\u672c\u8bed\u6cd5\u6982\u89c8<\/li>\n\n\n\n<li>\u7a0b\u5e8f\u7ed3\u6784\u590d\u4e60<\/li>\n\n\n\n<li>\u6570\u636e\u7c7b\u578b\uff08Data Types\uff09<\/li>\n\n\n\n<li>\u53d8\u91cf\u548c\u5e38\u91cf<\/li>\n\n\n\n<li>\u8fd0\u7b97\u7b26<\/li>\n\n\n\n<li>\u6761\u4ef6\u8bed\u53e5<\/li>\n\n\n\n<li>\u5faa\u73af\u7ed3\u6784<\/li>\n\n\n\n<li>\u65b9\u6cd5\uff08\u51fd\u6570\uff09\u5b9a\u4e49\u4e0e\u8c03\u7528<\/li>\n\n\n\n<li>\u7c7b\u4e0e\u5bf9\u8c61\u7684\u57fa\u672c\u4f7f\u7528<\/li>\n\n\n\n<li>\u6ce8\u91ca\u98ce\u683c<\/li>\n\n\n\n<li>\u547d\u540d\u89c4\u8303<\/li>\n\n\n\n<li>\u6743\u5a01\u94fe\u63a5\u4e0e\u5b98\u65b9\u6587\u6863<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1.  C# \u57fa\u672c\u8bed\u6cd5\u6982\u89c8<\/h2>\n\n\n\n<p>C# \u662f\u4e00\u79cd <strong>\u5f3a\u7c7b\u578b\u3001\u9762\u5411\u5bf9\u8c61<\/strong> \u7684\u8bed\u8a00\uff0c\u8bed\u6cd5\u7c7b\u4f3c Java \u548c C++\u3002\u4e3b\u8981\u8fd0\u884c\u5728 <a href=\"https:\/\/dotnet.microsoft.com\/\">.NET \u5e73\u53f0<\/a>\uff0c\u7528\u4e8e\u5f00\u53d1 Web\u3001\u684c\u9762\u3001\u79fb\u52a8\u3001\u6e38\u620f\u7b49\u5e94\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2.  \u7a0b\u5e8f\u7ed3\u6784\u590d\u4e60\uff08Hello World \u793a\u4f8b\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\nclass Program\n{\n    static void Main(string&#91;] args)\n    {\n        Console.WriteLine(\"Hello, World!\");\n    }\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>using<\/code>\uff1a\u5bfc\u5165\u547d\u540d\u7a7a\u95f4<\/li>\n\n\n\n<li><code>class Program<\/code>\uff1a\u7c7b\u662f C# \u7a0b\u5e8f\u7684\u57fa\u672c\u5355\u5143<\/li>\n\n\n\n<li><code>static void Main<\/code>\uff1a\u4e3b\u5165\u53e3\u65b9\u6cd5<\/li>\n\n\n\n<li><code>Console.WriteLine()<\/code>\uff1a\u63a7\u5236\u53f0\u8f93\u51fa<\/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\">3.  \u6570\u636e\u7c7b\u578b\uff08Data Types\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u27a4 \u503c\u7c7b\u578b\uff08Value Types\uff09<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u793a\u4f8b<\/th><\/tr><\/thead><tbody><tr><td><code>int<\/code><\/td><td>\u6574\u6570\uff0c\u5982 <code>123<\/code><\/td><\/tr><tr><td><code>float<\/code><\/td><td>\u5355\u7cbe\u5ea6\u6d6e\u70b9\u6570<\/td><\/tr><tr><td><code>double<\/code><\/td><td>\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570<\/td><\/tr><tr><td><code>char<\/code><\/td><td>\u5355\u4e2a\u5b57\u7b26 <code>'A'<\/code><\/td><\/tr><tr><td><code>bool<\/code><\/td><td>\u5e03\u5c14\u503c <code>true\/false<\/code><\/td><\/tr><tr><td><code>decimal<\/code><\/td><td>\u9ad8\u7cbe\u5ea6\u8d27\u5e01\u8ba1\u7b97<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u27a4 \u5f15\u7528\u7c7b\u578b\uff08Reference Types\uff09<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u793a\u4f8b<\/th><\/tr><\/thead><tbody><tr><td><code>string<\/code><\/td><td>\u5b57\u7b26\u4e32 <code>\"Hello\"<\/code><\/td><\/tr><tr><td><code>object<\/code><\/td><td>\u4e07\u80fd\u57fa\u7c7b<\/td><\/tr><tr><td>\u81ea\u5b9a\u4e49\u7c7b<\/td><td><code>Person<\/code>, <code>Car<\/code> \u7b49<\/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\">4.  \u53d8\u91cf\u4e0e\u5e38\u91cf<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>int age = 25;              \/\/ \u53d8\u91cf\nconst double PI = 3.14159; \/\/ \u5e38\u91cf\nstring name = \"Alice\";\nbool isAdmin = true;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5.  \u8fd0\u7b97\u7b26\uff08Operators\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u793a\u4f8b<\/th><\/tr><\/thead><tbody><tr><td>\u7b97\u672f\u8fd0\u7b97\u7b26<\/td><td><code>+ - * \/ %<\/code><\/td><\/tr><tr><td>\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/td><td><code>== != &gt; &lt; &gt;= &lt;=<\/code><\/td><\/tr><tr><td>\u903b\u8f91\u8fd0\u7b97\u7b26<\/td><td><code>&amp;&amp; || !<\/code><\/td><\/tr><tr><td>\u8d4b\u503c\u8fd0\u7b97\u7b26<\/td><td><code>= += -= *=<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>int x = 10;\nint y = 5;\nint sum = x + y;\nbool isGreater = x &gt; y;<\/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.  \u6761\u4ef6\u8bed\u53e5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u27a4 if-else<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>if (x &gt; 0)\n{\n    Console.WriteLine(\"Positive\");\n}\nelse\n{\n    Console.WriteLine(\"Non-positive\");\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u27a4 switch-case<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int day = 3;\n\nswitch (day)\n{\n    case 1:\n        Console.WriteLine(\"Mon\");\n        break;\n    case 2:\n        Console.WriteLine(\"Tue\");\n        break;\n    default:\n        Console.WriteLine(\"Other\");\n        break;\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.  \u5faa\u73af\u7ed3\u6784<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u27a4 for \u5faa\u73af<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = 0; i &lt; 5; i++)\n{\n    Console.WriteLine(i);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u27a4 while \u5faa\u73af<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int i = 0;\nwhile (i &lt; 5)\n{\n    Console.WriteLine(i);\n    i++;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u27a4 foreach\uff08\u9002\u7528\u4e8e\u96c6\u5408\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>string&#91;] fruits = { \"Apple\", \"Banana\", \"Cherry\" };\nforeach (string fruit in fruits)\n{\n    Console.WriteLine(fruit);\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.  \u65b9\u6cd5\u5b9a\u4e49\u4e0e\u8c03\u7528<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>static int Add(int a, int b)\n{\n    return a + b;\n}\n\nstatic void Main(string&#91;] args)\n{\n    int result = Add(3, 4);\n    Console.WriteLine(result); \/\/ \u8f93\u51fa 7\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\">9.  \u7c7b\u4e0e\u5bf9\u8c61\u7684\u57fa\u672c\u4f7f\u7528<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person\n{\n    public string Name;\n    public int Age;\n\n    public void Greet()\n    {\n        Console.WriteLine($\"Hi, I'm {Name}, {Age} years old.\");\n    }\n}\n\nclass Program\n{\n    static void Main(string&#91;] args)\n    {\n        Person p = new Person();\n        p.Name = \"Alice\";\n        p.Age = 30;\n        p.Greet();\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\">10.  \u6ce8\u91ca\u98ce\u683c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5355\u884c\u6ce8\u91ca\n\n\/*\n \u591a\u884c\u6ce8\u91ca\n \u53ef\u4ee5\u5199\u591a\u884c\n*\/\n\n\/\/\/ &lt;summary&gt;\n\/\/\/ \u7528\u4e8e\u751f\u6210 XML \u6587\u6863\u7684\u6ce8\u91ca\n\/\/\/ &lt;\/summary&gt;\npublic void MyMethod() {}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">11.  \u547d\u540d\u89c4\u8303\uff08Coding Conventions\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9879\u76ee<\/th><th>\u63a8\u8350\u65b9\u5f0f<\/th><\/tr><\/thead><tbody><tr><td>\u7c7b\u540d<\/td><td>PascalCase<\/td><\/tr><tr><td>\u65b9\u6cd5\u540d<\/td><td>PascalCase<\/td><\/tr><tr><td>\u53d8\u91cf\u540d<\/td><td>camelCase<\/td><\/tr><tr><td>\u5e38\u91cf\u540d<\/td><td>UPPER_SNAKE_CASE<\/td><\/tr><tr><td>\u547d\u540d\u7a7a\u95f4\u540d<\/td><td>\u516c\u53f8.\u9879\u76ee.\u6a21\u5757<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udcd8 \u5b98\u65b9\u547d\u540d\u89c4\u8303\uff1a<br>\ud83d\udc49 <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/fundamentals\/coding-style\/coding-conventions\">C# Coding Conventions &#8211; Microsoft<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">12.  \u5b98\u65b9\u6743\u5a01\u6587\u6863\u4e0e\u5b66\u4e60\u8def\u5f84<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5185\u5bb9<\/th><th>\u94fe\u63a5<\/th><\/tr><\/thead><tbody><tr><td>C# \u8bed\u6cd5\u57fa\u7840<\/td><td>https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/fundamentals\/<\/td><\/tr><tr><td>\u6570\u636e\u7c7b\u578b<\/td><td>https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/builtin-types\/built-in-types<\/td><\/tr><tr><td>\u63a7\u5236\u6d41<\/td><td>https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/statements\/selection-statements<\/td><\/tr><tr><td>\u65b9\u6cd5\u5b9a\u4e49<\/td><td>https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/methods<\/td><\/tr><tr><td>\u7c7b\u4e0e\u5bf9\u8c61<\/td><td>https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/classes<\/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\">\u603b\u7ed3\u56de\u987e<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5185\u5bb9<\/th><th>\u6838\u5fc3\u77e5\u8bc6\u70b9<\/th><\/tr><\/thead><tbody><tr><td>\u6570\u636e\u7c7b\u578b<\/td><td>\u503c\u7c7b\u578b vs \u5f15\u7528\u7c7b\u578b<\/td><\/tr><tr><td>\u63a7\u5236\u7ed3\u6784<\/td><td>if\u3001switch\u3001for\u3001while\u3001foreach<\/td><\/tr><tr><td>\u65b9\u6cd5<\/td><td>\u53c2\u6570\u4f20\u9012\u3001\u8fd4\u56de\u503c<\/td><\/tr><tr><td>\u9762\u5411\u5bf9\u8c61<\/td><td>\u7c7b\u3001\u5b57\u6bb5\u3001\u65b9\u6cd5<\/td><\/tr><tr><td>\u547d\u540d<\/td><td>PascalCase \/ camelCase<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udccc \u76ee\u5f55\u7ed3\u6784 1. C# \u57fa\u672c\u8bed\u6cd5\u6982\u89c8 C# \u662f\u4e00\u79cd \u5f3a\u7c7b\u578b\u3001\u9762\u5411\u5bf9\u8c61 \u7684\u8bed\u8a00\uff0c\u8bed\u6cd5\u7c7b\u4f3c Java \u548c C++ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3471,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3470","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\/3470","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=3470"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3470\/revisions"}],"predecessor-version":[{"id":3472,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3470\/revisions\/3472"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3471"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}