{"id":121,"date":"2008-05-01T14:59:38","date_gmt":"2008-05-01T14:59:38","guid":{"rendered":""},"modified":"2008-05-01T14:59:38","modified_gmt":"2008-05-01T14:59:38","slug":"121","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=121","title":{"rendered":"LINQ\u4f53\u9a8c(2)\u2014\u2014C# 3.0\u65b0\u8bed\u8a00\u7279\u6027\u548c\u6539\u8fdb(\u4e0a\u7bc7)"},"content":{"rendered":"<p>\u5728\u7b2c\u4e00\u7bc7\u4e2d\uff0c\u77e5\u9053\u4e86<font color=\"#000000\">Visual Studio 2008\u65b0\u7279\u6027<\/font>\uff0c\u4ece\u8fd9\u7bc7\u5f00\u59cb\u8fdb\u5165\u6b64\u7cfb\u5217\u7684\u7b2c\u4e8c\u90e8\u5206&mdash;&mdash;\u4ecb\u7ecdC# 3.0\u65b0\u8bed\u8a00\u7279\u6027\u548c\u6539\u8fdb\u3002<\/p>\n<p>\u603b\u4f53\u6765\u8bf4\uff0cVisual Studio 2008\u548c.NET 3.5\u662f\u5efa\u7acb\u5728.NET2.0\u6838\u5fc3\u7684\u57fa\u7840\u4e4b\u4e0a\uff0c.NET2.0\u6838\u5fc3\u672c\u8eab\u5c06\u4e0d\u518d\u53d8\u5316\uff08\u5982\u679c\u4e0d\u4e86\u89e3.NET2.0\u7684\u670b\u53cb\uff0c\u8bf7\u53c2\u770bMSDN\u6216\u8005\u4e00\u4e9b\u7ecf\u5178\u7684\u4e66\u7c4d\uff09\uff0cC# 3.0\u65b0\u8bed\u8a00\u7279\u6027\u5728.NET2.0\u57fa\u7840\u4e0a\u8fdb\u884c\u4e86\u6539\u8fdb\uff0c\u8fd9\u4e9b\u6539\u8fdb\u7684\u529f\u80fd\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u6211\u4eec\u7f16\u5199\u7a0b\u5e8f\u3002\u5173\u4e8eC# 3.0\u65b0\u8bed\u8a00\u7279\u6027\u5728\u535a\u5ba2\u56ed\u91cc\u7684\u5f88\u591a\u670b\u53cb\u90fd\u4ecb\u7ecd\u4e86\uff0c\u6211\u5728\u8fd9\u91cc\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u8bb0\u5f55\u81ea\u5df1\u6240\u5b66\u7684\u4e1c\u897f\uff0c\u4e5f\u4e3a\u540e\u9762\u7684LINQ\u6253\u4e0b\u57fa\u7840\u3002<\/p>\n<p>C# 3.0\u65b0\u8bed\u8a00\u7279\u6027\u548c\u6539\u8fdb\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u81ea\u52a8\u5c5e\u6027(Auto-Implemented Properties) <\/li>\n<li>\u9690\u542b\u7c7b\u578b\u5c40\u90e8\u53d8\u91cf(Local Variable Type Inference) <\/li>\n<li>\u533f\u540d\u7c7b\u578b(Anonymous Types) <\/li>\n<li>\u5bf9\u8c61\u4e0e\u96c6\u5408\u521d\u59cb\u5316\u5668(Object and Collection Initializers) <\/li>\n<li>\u6269\u5c55\u65b9\u6cd5(Extension Methods) <\/li>\n<li>Lambda\u8868\u8fbe\u5f0f\u548cLambda\u8868\u8fbe\u5f0f\u6811 (Lambda Expression and Lambda Expression Trees) <\/li>\n<\/ul>\n<h2 class=\"hcode\">\u81ea\u52a8\u5c5e\u6027(Auto-Implemented Properties)<\/h2>\n<p>\u81ea\u52a8\u5c5e\u6027\u53ef\u4ee5\u907f\u514d\u539f\u6765\u8fd9\u6837\u6211\u4eec\u624b\u5de5\u58f0\u660e\u4e00\u4e2a\u79c1\u6709\u6210\u5458\u53d8\u91cf\u4ee5\u53ca\u7f16\u5199get\/set\u903b\u8f91\uff0c\u5728VS2008\u4e2d\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u7f16\u5199\u4e00\u4e2a\u7c7b\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5730\u751f\u6210\u79c1\u6709\u53d8\u91cf\u548c\u9ed8\u8ba4\u7684get\/set \u64cd\u4f5c\u3002\u4f60\u4e5f\u53ef\u4ee5\u5206\u522b\u5b9a\u4e49get\u548cset\u7684&ldquo;protected&rdquo;\u7b49\u8bbf\u95ee\u7ea7\u522b\u3002<\/p>\n<p>\u5728.Net2.0\u6846\u67b6\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5199\u4e00\u4e2aUser\u7c7b\uff1a <\/p>\n<pre class=\"code\"><span style=\"COLOR: blue\">public class <\/span><span style=\"COLOR: #2b91af\">User\r\n<\/span>{\r\n<span style=\"COLOR: blue\">private int <\/span>_id;\r\n<span style=\"COLOR: blue\">private string <\/span>_name;\r\n<span style=\"COLOR: blue\">private int <\/span>_age;\r\n<span style=\"COLOR: blue\">public int <\/span>Id\r\n{\r\n<span style=\"COLOR: blue\">get <\/span>{ <span style=\"COLOR: blue\">return <\/span>_id; }\r\n<span style=\"COLOR: blue\">set <\/span>{ _id = <span style=\"COLOR: blue\">value<\/span>; }\r\n}\r\n<span style=\"COLOR: blue\">public string <\/span>Name\r\n{\r\n<span style=\"COLOR: blue\">get <\/span>{ <span style=\"COLOR: blue\">return <\/span>_name; }\r\n<span style=\"COLOR: blue\">set <\/span>{ _name = <span style=\"COLOR: blue\">value<\/span>; }\r\n}\r\n<span style=\"COLOR: blue\">public int <\/span>Age\r\n{\r\n<span style=\"COLOR: blue\">get <\/span>{ <span style=\"COLOR: blue\">return <\/span>_age; }\r\n<span style=\"COLOR: blue\">set <\/span>{ _age = <span style=\"COLOR: blue\">value<\/span>; }\r\n}\r\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u53ef\u4ee5\u8fd9\u6837\u7b80\u5316\uff1a<\/p>\n<pre class=\"code\"><span style=\"COLOR: blue\">public class <\/span><span style=\"COLOR: #2b91af\">User\r\n<\/span>{\r\n<span style=\"COLOR: blue\">public int <\/span>Id { <span style=\"COLOR: blue\">get<\/span>; <span style=\"COLOR: blue\">set<\/span>; }\r\n<span style=\"COLOR: blue\">public string <\/span>Name { <span style=\"COLOR: blue\">get<\/span>; <span style=\"COLOR: blue\">set<\/span>; }\r\n<span style=\"COLOR: blue\">public int <\/span>Age { <span style=\"COLOR: blue\">get<\/span>; <span style=\"COLOR: blue\">set<\/span>; }\r\n}<\/pre>\n<p>\u50cf\u4e0a\u9762\u8fd9\u6837\u7684\u7a7a\u7684get\/set\u5c5e\u6027\u7684\u8bdd\uff0c\u5b83\u4f1a\u81ea\u52a8\u4e3a\u4f60\u5728\u7c7b\u4e2d\u751f\u6210\u4e00\u4e2a\u79c1\u6709\u6210\u5458\u53d8\u91cf\uff0c\u5bf9\u8fd9\u4e2a\u53d8\u91cf\u5b9e\u73b0\u4e00\u4e2a\u516c\u5f00\u7684getter \u548csetter\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528.NET\u5f00\u53d1\u73af\u5883\u6240\u63d0\u4f9b\u7684ildasm.exe\uff08IL\u4ee3\u7801\u53cd\u6c47\u7f16\u5668\uff09\u5de5\u5177\u6765\u5206\u6790\u7a0b\u5e8f\u96c6\u6216\u8005\u6a21\u5757\u7684\u5185\u5bb9\u3002\u6211\u5c31\u4e0d\u8d34\u56fe\u4e86\u3002<\/p>\n<h2 class=\"hcode\">\u9690\u542b\u7c7b\u578b\u5c40\u90e8\u53d8\u91cf(Local Variable Type Inference)<\/h2>\n<p>C#3.0\u5f15\u8fdb\u4e86var\u8fd9\u4e2a\u65b0\u5173\u952e\u5b57\uff0c\u5728\u58f0\u660e\u5c40\u90e8\u53d8\u91cf\u65f6\u53ef\u7528\u4e8e\u66ff\u4ee3\u539f\u5148\u7684\u7c7b\u578b\u540d\uff0c\u5373\u5f53\u4e00\u4e2a\u53d8\u91cf\u58f0\u660e\u6807\u8bc6\u4e3avar\u7c7b\u578b\u5e76\u4e14\u8be5\u8303\u56f4\u57df\u4e2d\u6ca1\u6709var\u540d\u79f0\u7c7b\u578b\u5b58\u5728\uff0c\u90a3\u4e48\u8fd9\u4e2a\u58f0\u660e\u5c31\u79f0\u4e3a\u9690\u542b\u7c7b\u578b\u5c40\u90e8\u53d8\u91cf\u3002\u5982\u4e0b\uff08\u7b49\u540c\u4e8e\/\/\u540e\u9762\u7684\u663e\u5f0f\u58f0\u660e\uff09\uff1a<\/p>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>i = 5;<span style=\"COLOR: green\">\/\/int<\/span>\r\n<span style=\"COLOR: blue\">var <\/span>j = 23.56;<span style=\"COLOR: green\">\/\/double<\/span>\r\n<span style=\"COLOR: blue\">var <\/span>k = <span style=\"COLOR: #a31515\">&quot;C Sharp&quot;<\/span>;<span style=\"COLOR: green\">\/\/string<\/span>\r\n<span style=\"COLOR: blue\">var <\/span>x;<span style=\"COLOR: green\">\/\/\u9519\u8bef<\/span>\r\n<span style=\"COLOR: blue\">var <\/span>y = <span style=\"COLOR: blue\">null<\/span>;<span style=\"COLOR: green\">\/\/\u9519\u8bef<\/span>\r\n<span style=\"COLOR: blue\">var <\/span>z = { 1, 2, 3 };<span style=\"COLOR: green\">\/\/\u9519\u8bef<\/span><\/pre>\n<p>\u5728\u8c03\u8bd5\u72b6\u6001\u4e0b\uff0c\u7f16\u8bd1\u5668\u89e3\u91ca\u5982\u4e0b<img decoding=\"async\" alt=\"\" src=\"http:\/\/www.poloo.org\/attachments\/month_0805\/e2008519145718.gif\" \/><\/p>\n<p><a href=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ3.gif\" target=\"_blank\"><\/a><\/p>\n<p><em>\u9690\u542b\u7c7b\u578b\u5c40\u90e8\u53d8\u91cf\u8981\u70b9<\/em><\/p>\n<ol>\n<li>var\u4e3a\u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u6839\u636e\u540e\u9762\u7684\u521d\u59cb\u5316\u8bed\u53e5\u81ea\u52a8\u63a8\u65ad\u7c7b\u578b\uff0c\u8fd9\u4e2a\u7c7b\u578b\u4e3a\u5f3a\u7c7b\u578b\u3002 <\/li>\n<li>\u521d\u59cb\u5316\u8bed\u53e5\u5fc5\u987b\u4e3a\u8868\u8fbe\u5f0f\uff0c\u4e0d\u53ef\u4ee5\u4e3a\u7a7a\u3002\u4e14\u7f16\u8bd1\u65f6\u53ef\u4ee5\u63a8\u65ad\u7c7b\u578b\u3002\u4e00\u65e6\u521d\u59cb\u5316\u4e4b\u540e\uff0c\u53ea\u53ef\u4ee5\u5b58\u50a8\u8fd9\u79cd\u7c7b\u578b\u3002 <\/li>\n<li>var\u58f0\u660e\u7684\u4ec5\u9650\u4e8e\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0d\u53ef\u7528\u4e8e\u5b57\u6bb5\u3002\u4ea6\u53ef\u4ee5\u7528\u4e8efor\uff0cforeach\uff0cusing \u7b49\u8bed\u53e5\u4e2d\u3002 <\/li>\n<li>\u6570\u7ec4\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u9690\u542b\u7c7b\u578b\u3002 <\/li>\n<li>\u521d\u59cb\u5316\u8bed\u53e5\u4e0d\u80fd\u662f\u4e00\u4e2a\u81ea\u8eab\u7684\u5bf9\u8c61\u6216\u8005\u96c6\u5408\u521d\u59cb\u5316\u5668\uff0c\u4f46\u662f\u4ed6\u53ef\u4ee5\u662f\u5305\u542b\u4e00\u4e2a\u5bf9\u8c61\u6216\u8005\u521d\u59cb\u5316\u5668\u7684\u4e00\u4e2anew\u8868\u8fbe\u5f0f\u3002 <\/li>\n<li>\u5982\u679c\u5c40\u90e8\u53d8\u91cf\u58f0\u660e\u5305\u542b\u4e86\u591a\u4e2a\u58f0\u660e\u7b26\uff0c\u5176\u7c7b\u578b\u5fc5\u987b\u76f8\u540c\u3002 <\/li>\n<\/ol>\n<h2 class=\"hcode\">\u533f\u540d\u7c7b\u578b(Anonymous Types)<\/h2>\n<p>\u533f\u540d\u7c7b\u578b\u5141\u8bb8\u5b9a\u4e49\u884c\u5185\u7c7b\u578b\uff0c\u65e0\u987b\u663e\u5f0f\u5b9a\u4e49\u7c7b\u578b\u3002\u5e38\u548cvar\u914d\u5408\u4f7f\u7528\u6765\u58f0\u660e\u533f\u540d\u7c7b\u578b\u3002<\/p>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>p1 = <span style=\"COLOR: blue\">new <\/span>{ Id = 1, Name = <span style=\"COLOR: #a31515\">&quot;YJingLee&quot;<\/span>, Age = 22 };<span style=\"COLOR: green\">\/\/\u5c5e\u6027\u4e5f\u4e0d\u9700\u8981\u7533\u660e<\/span>\r\n<span style=\"COLOR: blue\">var <\/span>p2 = <span style=\"COLOR: blue\">new <\/span>{ Id = 2, Name = <span style=\"COLOR: #a31515\">&quot;XieQing&quot;<\/span>, Age = 25 };\r\np1 = p2;<span style=\"COLOR: green\">\/\/p1,p2\u7ed3\u6784\u76f8\u540c\uff0c\u53ef\u4ee5\u4e92\u76f8\u8d4b\u503c<\/span><\/pre>\n<p><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.poloo.org\/attachments\/month_0805\/v2008519145748.gif\" \/><\/p>\n<p>\u5728\u8fd9\u91cc\u7f16\u8bd1\u5668\u4f1a\u8ba4\u4e3ap1,p2\u76f8\u5f53\u4e8e\uff1a<\/p>\n<pre class=\"code\"><span style=\"COLOR: blue\">public class <\/span><span style=\"COLOR: #2b91af\">SomeType<\/span>\r\n{\r\n<span style=\"COLOR: blue\">public int <\/span>Id { <span style=\"COLOR: blue\">get<\/span>; <span style=\"COLOR: blue\">set<\/span>; }\r\n<span style=\"COLOR: blue\">public string <\/span>Name { <span style=\"COLOR: blue\">get<\/span>; <span style=\"COLOR: blue\">set<\/span>; }\r\n<span style=\"COLOR: blue\">public int <\/span>Age { <span style=\"COLOR: blue\">get<\/span>; <span style=\"COLOR: blue\">set<\/span>; }\r\n}<\/pre>\n<p>\u90a3\u4e48\u6570\u7ec4\u600e\u4e48\u5b9a\u4e49\u5462\uff1f\u4f7f\u7528&quot;new[]&quot;\u5173\u952e\u5b57\u6765\u58f0\u660e\u6570\u7ec4\uff0c\u52a0\u4e0a\u6570\u7ec4\u7684\u521d\u59cb\u503c\u5217\u8868\u3002\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>intArray = <span style=\"COLOR: blue\">new<\/span>[] { 2, 3, 5, 6 };\r\n<span style=\"COLOR: blue\">var <\/span>strArray = <span style=\"COLOR: blue\">new<\/span>[] { <span style=\"COLOR: #a31515\">&quot;Hello&quot;<\/span>, <span style=\"COLOR: #a31515\">&quot;World&quot; <\/span>};\r\n<span style=\"COLOR: blue\">var <\/span>anonymousTypeArray = <span style=\"COLOR: blue\">new<\/span>[]\r\n{\r\n<span style=\"COLOR: blue\">new <\/span>{ Name = <span style=\"COLOR: #a31515\">&quot;YJingLee&quot;<\/span>, Age = 22 },\r\n<span style=\"COLOR: blue\">new <\/span>{ Name = <span style=\"COLOR: #a31515\">&quot;XieQing&quot;<\/span>, Age = 25 }\r\n};\r\n<span style=\"COLOR: blue\">var <\/span>a = intArray[0];\r\n<span style=\"COLOR: blue\">var <\/span>b = strArray[0];\r\n<span style=\"COLOR: blue\">var <\/span>c = anonymousTypeArray[1].Name;<br \/><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.poloo.org\/attachments\/month_0805\/x2008519145831.gif\" \/><\/pre>\n<p><em>\u533f\u540d\u7c7b\u578b\u8981\u70b9<\/em><\/p>\n<ol>\n<li>\u53ef\u4ee5\u4f7f\u7528new\u5173\u952e\u5b57\u8c03\u7528\u533f\u540d\u521d\u59cb\u5316\u5668\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 <\/li>\n<li>\u533f\u540d\u7c7b\u578b\u76f4\u63a5\u7ee7\u627f\u81eaSystem. Object\u3002 <\/li>\n<li>\u533f\u540d\u7c7b\u578b\u7684\u6210\u5458\u662f\u7f16\u8bd1\u5668\u6839\u636e\u521d\u59cb\u5316\u5668\u63a8\u65ad\u800c\u6765\u7684\u4e00\u4e9b\u8bfb\u5199\u5c5e\u6027\u3002 <\/li>\n<\/ol>\n<h2 class=\"hcode\">\u5bf9\u8c61\u4e0e\u96c6\u5408\u521d\u59cb\u5316\u5668(Object and Collection Initializers)<\/h2>\n<h4>\u5bf9\u8c61\u521d\u59cb\u5316\u5668 (Object Initializers) \uff1a<\/h4>\n<p>.NET2.0\u6846\u67b6\u4e2d\u7684\u7c7b\u578b\u975e\u5e38\u4f9d\u8d56\u4e8e\u5c5e\u6027\u3002\u5f53\u751f\u6210\u5bf9\u8c61\u5b9e\u4f8b\u548c\u4f7f\u7528\u65b0\u7684\u7c7b\u578b\u65f6\uff0c\u5728.Net2.0\u65f6\u5019\u6211\u4eec\u50cf\u8fd9\u6837\u5199\uff1a<\/p>\n<pre class=\"code\"><span style=\"COLOR: #2b91af\">User <\/span>user = <span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">User<\/span>();\r\nuser.Id = 1;\r\nuser.Name = <span style=\"COLOR: #a31515\">&quot;YJingLee&quot;<\/span>;\r\nuser.Age = 22;<\/pre>\n<p>\u5728VS2008\u4e2d\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5730\u751f\u6210\u5408\u9002\u7684\u5c5e\u6027setter\u4ee3\u7801\uff0c\u4f7f\u5f97\u539f\u6765\u51e0\u884c\u7684\u5c5e\u6027\u8d4b\u503c\u64cd\u4f5c\u53ef\u4ee5\u5728\u4e00\u884c\u5b8c\u6210\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u7b80\u5316\uff1a\u50cf\u8fd9\u6837\uff0c<strong>\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u7531\u4e00\u7cfb\u5217\u6210\u5458\u5bf9\u8c61\u7ec4\u6210\uff0c\u5176\u5bf9\u8c61\u5fc5\u987b\u521d\u59cb\u5316\uff0c\u7528\u9017\u53f7\u95f4\u9694\uff0c\u4f7f\u7528{}\u5c01\u95ed\u3002<\/strong><\/p>\n<pre class=\"code\"><span style=\"COLOR: #2b91af\">User <\/span>user = <span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">User <\/span>{ Id = 1, Name = <span style=\"COLOR: #a31515\">&quot;YJingLee&quot;<\/span>, Age = 22 };<\/pre>\n<p>\u53c8\u4f8b\u5982\uff0c\u6211\u628a\u4e8c\u4e2a\u4eba\u52a0\u5230\u4e00\u4e2a\u57fa\u4e8e\u6cdb\u578b\u7684\u7c7b\u578b\u4e3aUser\u7684List\u96c6\u5408\u4e2d\uff1a<\/p>\n<pre class=\"code\">List&lt;<span style=\"COLOR: #2b91af\">User<\/span>&gt; user = <span style=\"COLOR: blue\">new <\/span>List&lt;<span style=\"COLOR: #2b91af\">User<\/span>&gt;{\r\n<span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">User<\/span>{Id=1,Name=<span style=\"COLOR: #a31515\">&quot;YJingLee&quot;<\/span>,Age=22},\r\n<span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">User<\/span>{Id=2,Name=<span style=\"COLOR: #a31515\">&quot;XieQing&quot;<\/span>,Age=25},\r\n};<br \/><\/pre>\n<p><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.poloo.org\/attachments\/month_0805\/x200851914591.gif\" \/><\/p>\n<p>\u5982\u679c\u6709\u76f8\u540c\u540d\u5b57\u548c\u7c7b\u578b\u7684\u4e24\u4e2a\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u5c06\u4f1a\u4ea7\u751f\u76f8\u540c\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u76f8\u4e92\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"code\"><span style=\"COLOR: #2b91af\">User <\/span>user = <span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">User <\/span>{ Id = 1, Name = <span style=\"COLOR: #a31515\">&quot;YJingLee&quot;<\/span>, Age = 22 };\r\n<span style=\"COLOR: #2b91af\">User <\/span>user2 = <span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">User <\/span>{ Id = 2, Name = <span style=\"COLOR: #a31515\">&quot;XieQing&quot;<\/span>, Age = 25 };\r\nuser = user2; <\/pre>\n<p>\u9664\u4e86\u5728\u521d\u59cb\u5316\u7c7b\u65f6\u8bbe\u7f6e\u7b80\u5355\u7684\u5c5e\u6027\u503c\u5916\uff0c\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u7279\u6027\u4e5f\u5141\u8bb8\u6211\u4eec\u8bbe\u7f6e\u66f4\u590d\u6742\u7684\u5d4c\u5957(nested)\u5c5e\u6027\u7c7b\u578b\u3002\u4f8b\u5982\u6211\u4eec\u53ef\u4ee5\u5728\u4e0a\u9762\u5b9a\u4e49\u7684User\u7c7b\u578b\u540c\u65f6\u62e5\u6709\u4e00\u4e2a\u5c5e\u4e8eAddress\u7c7b\u578b\u7684\u53eb&ldquo;Address&rdquo;\u7684\u5c5e\u6027\uff1a<\/p>\n<pre class=\"code\"><span style=\"COLOR: #2b91af\">User<\/span> user = <span style=\"COLOR: #0000ff\">new<\/span> <span style=\"COLOR: #2b91af\">User<\/span>\r\n{\r\nId = 1,\r\nName = <span style=\"COLOR: #8b0000\">&quot;YJingLee&quot;<\/span>,\r\nAge = 22,\r\n<span style=\"COLOR: #2b91af\">Address<\/span> = <span style=\"COLOR: #0000ff\">new<\/span> <span style=\"COLOR: #2b91af\">Address<\/span>\r\n{\r\nCity = <span style=\"COLOR: #8b0000\">&quot;NanJing&quot;<\/span>,\r\nZip = 21000\r\n}\r\n};<\/pre>\n<h4 class=\"hcode\">\u96c6\u5408\u521d\u59cb\u5316\u5668(Collection Initializers)\uff1a<\/h4>\n<p><strong>\u96c6\u5408\u521d\u59cb\u5316\u5668\u7531\u4e00\u7cfb\u5217\u96c6\u5408\u5bf9\u8c61\u7ec4\u6210\uff0c\u7528\u9017\u53f7\u95f4\u9694\uff0c\u4f7f\u7528{}\u5c01\u95ed\u3002<\/strong><br \/>\u96c6\u5408\u521d\u59cb\u5316\u5668\u53ef\u4ee5\u7b80\u5316\u628a\u51e0\u4e2a\u5bf9\u8c61\u4e00\u8d77\u6dfb\u52a0\u5230\u4e00\u4e2a\u96c6\u5408\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u4e3a\u4f60\u505a\u96c6\u5408\u63d2\u5165\u64cd\u4f5c\u3002\u4f8b\u5982\u6211\u628a\u4e03\u4e2a\u6570\u52a0\u5230\u4e00\u4e2a\u57fa\u4e8e\u6cdb\u578b\u7684\u7c7b\u578b\u4e3aint\u7684List\u96c6\u5408\u4e2d<\/p>\n<pre class=\"code\"><span style=\"COLOR: #2b91af\">List<\/span>&lt;<span style=\"COLOR: blue\">int<\/span>&gt; num = <span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">List<\/span>&lt;<span style=\"COLOR: blue\">int<\/span>&gt; { 0, 1, 2, 6, 7, 8, 9 };<\/pre>\n<p><em>\u5bf9\u8c61\u4e0e\u96c6\u5408\u521d\u59cb\u5316\u5668\u8981\u70b9<\/em><\/p>\n<ol>\n<li>\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u5b9e\u9645\u4e0a\u5229\u7528\u4e86\u7f16\u8bd1\u5668\u5bf9\u5bf9\u8c61\u4e2d\u5bf9\u5916\u53ef\u89c1\u7684\u5b57\u6bb5\u548c\u5c5e\u6027\u8fdb\u884c\u6309\u5e8f\u8d4b\u503c\u3002 <\/li>\n<li>\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u5141\u8bb8\u53ea\u7ed9\u4e00\u90e8\u5206\u5c5e\u6027\u8d4b\u503c\uff0c\u5305\u62ecinternal\u8bbf\u95ee\u7ea7\u522b <\/li>\n<li>\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u53ef\u4ee5\u7ed3\u5408\u6784\u9020\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u5e76\u4e14\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u5148\u4e8e\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u6267\u884c\u3002 <\/li>\n<li>\u96c6\u5408\u521d\u59cb\u5316\u5668\u4f1a\u5bf9\u521d\u59cb\u5316\u5668\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6309\u5e8f\u8c03\u7528ICollection&lt;T&gt;.Add(T)\u65b9\u6cd5\u3002 <\/li>\n<li>\u6ce8\u610f\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u548c\u96c6\u5408\u521d\u59cb\u5316\u5668\u4e2d\u6210\u5458\u7684\u53ef\u89c1\u6027\u548c\u8c03\u7528\u987a\u5e8f\u3002 <\/li>\n<li>\u5bf9\u8c61\u4e0e\u96c6\u5408\u521d\u59cb\u5316\u5668\u540c\u6837\u662f\u4e00\u79cd\u7f16\u8bd1\u65f6\u6280\u672f\u3002 <\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7b2c\u4e00\u7bc7\u4e2d\uff0c\u77e5\u9053\u4e86Visual Studio 2008\u65b0\u7279\u6027\uff0c\u4ece\u8fd9\u7bc7&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-NET"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=121"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/121\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}