{"id":79,"date":"2008-02-15T00:02:47","date_gmt":"2008-02-15T00:02:47","guid":{"rendered":""},"modified":"2008-02-15T00:02:47","modified_gmt":"2008-02-15T00:02:47","slug":"79","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=79","title":{"rendered":"C# \u7f16\u7801\u89c4\u8303\u548c\u7f16\u7a0b\u597d\u4e60\u60ef"},"content":{"rendered":"<p>\u64b0\u6587\uff1aDotNetSpider.com\uff08<a href=\"http:\/\/www.dotnetspider.com\" target=\"_blank\" rel=\"external\">http:\/\/www.dotnetspider.com<\/a>\uff09<\/p>\n<p>\u7ffb\u8bd1\uff1a\u6768\u8d3a\u5b8f<\/p>\n<p>\u8c01\u90fd\u4f1a\u5199\u4ee3\u7801\uff01\u51e0\u4e2a\u6708\u7684\u7f16\u7a0b\u7ecf\u9a8c\u53ef\u4ee5\u8ba9\u4f60\u5199\u51fa\u201c\u53ef\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u201d\u3002\u8ba9\u5b83\u53ef\u8fd0\u884c\u5bb9\u6613\uff0c\u4f46\u662f\u4ee5\u6700\u6709\u6548\u7387\u7684\u65b9\u5f0f\u7f16\u7801\u5c31\u9700\u8981\u4e0b\u66f4\u591a\u7684\u529f\u592b\uff01<\/p>\n<p>\u8981\u77e5\u9053\uff0c\u5927\u591a\u6570\u7a0b\u5e8f\u5458\u5728\u5199\u201d\u53ef\u8fd0\u884c\u4ee3\u7801\uff0c\u201c\u800c\u4e0d\u662f\u201d\u9ad8\u6548\u4ee3\u7801\u201c\u3002\u6211\u4eec\u5728\u8fd9\u4e2a\u6307\u5357\u8bfe\u7a0b\u524d\u9762\u63d0\u5230\uff0c\u4f60\u60f3\u6210\u4e3a\u4f60\u4eec\u516c\u53f8\u201d\u6700\u5c0a\u8d35\u7684\u4e13\u4e1a\u4eba\u5458\u201c\u5417\uff1f\u5199\u201d\u9ad8\u6548\u4ee3\u7801\u201c\u662f\u4e00\u9879\u827a\u672f\uff0c\u4f60\u5fc5\u987b\u5b66\u4e60\u548c\u5b9e\u8df5\u5b83\u3002<\/p>\n<p>\u547d\u540d\u60ef\u4f8b\u548c\u89c4\u8303<\/p>\n<p>\u6ce8\u8bb0 : <br \/>Pascal \u5927\u5c0f\u5199\u5f62\u5f0f\uff0d\u6240\u6709\u5355\u8bcd\u7b2c\u4e00\u4e2a\u5b57\u6bcd\u5927\u5199\uff0c\u5176\u4ed6\u5b57\u6bcd\u5c0f\u5199\u3002<br \/>Camel&nbsp;&nbsp; \u5927\u5c0f\u5199\u5f62\u5f0f\uff0d\u9664\u4e86\u7b2c\u4e00\u4e2a\u5355\u8bcd\uff0c\u6240\u6709\u5355\u8bcd\u7b2c\u4e00\u4e2a\u5b57\u6bcd\u5927\u5199\uff0c\u5176\u4ed6\u5b57\u6bcd\u5c0f\u5199\u3002<\/p>\n<p>\u7c7b\u540d\u4f7f\u7528Pascal \u5927\u5c0f\u5199\u5f62\u5f0f <br \/>public class HelloWorld{ &#8230;}<\/p>\n<p>\u65b9\u6cd5\u4f7f\u7528Pascal \u5927\u5c0f\u5199\u5f62\u5f0f <br \/>public class HelloWorld{ void SayHello(string name) {&nbsp;&nbsp;&#8230; }}<br \/>\u53d8\u91cf\u548c\u65b9\u6cd5\u53c2\u6570\u4f7f\u7528Camel \u5927\u5c0f\u5199\u5f62\u5f0f <\/p>\n<p>public class HelloWorld{ int totalCount = 0; void SayHello(string name) {&nbsp;&nbsp;string fullMessage = &#8220;Hello &#8221; + name;&nbsp;&nbsp;&#8230; }}&nbsp;&nbsp;\u4e0d\u8981\u4f7f\u7528\u5308\u7259\u5229\u65b9\u6cd5\u6765\u547d\u540d\u53d8\u91cf <\/p>\n<p>\u4ee5\u524d\uff0c\u591a\u6570\u7a0b\u5e8f\u5458\u559c\u6b22\u5b83\uff0d\u628a\u6570\u636e\u7c7b\u578b\u4f5c\u4e3a\u53d8\u91cf\u540d\u7684\u524d\u7f00\u800cm_\u4f5c\u4e3a\u6210\u5458\u53d8\u91cf\u7684\u524d\u7f00\u3002\u4f8b\u5982\uff1a <br \/>string m_sName;int nAge;&nbsp;&nbsp;&nbsp;&nbsp;\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5728.NET\u7f16\u7801\u89c4\u8303\u4e2d\u662f\u4e0d\u63a8\u8350\u7684\u3002\u6240\u6709\u53d8\u91cf\u90fd\u7528camel \u5927\u5c0f\u5199\u5f62\u5f0f\uff0c\u800c\u4e0d\u662f\u7528\u6570\u636e\u7c7b\u578b\u548cm_\u6765\u4f5c\u524d\u7f00\u3002 <\/p>\n<p>\u7528\u6709\u610f\u4e49\u7684\uff0c\u63cf\u8ff0\u6027\u7684\u8bcd\u8bed\u6765\u547d\u540d\u53d8\u91cf <\/p>\n<p>&#8211; \u522b\u7528\u7f29\u5199\u3002\u7528name, address, salary\u7b49\u4ee3\u66ff nam, addr, sal <br \/>&#8211; \u522b\u4f7f\u7528\u5355\u4e2a\u5b57\u6bcd\u7684\u53d8\u91cf\u8c61i, n, x \u7b49. \u4f7f\u7528 index, temp\u7b49 <br \/>\u7528\u4e8e\u5faa\u73af\u8fed\u4ee3\u7684\u53d8\u91cf\u4f8b\u5916\uff1a <br \/>for ( int i = 0; i &lt; count; i++ ){ &#8230;}\u5982\u679c\u53d8\u91cf\u53ea\u7528\u4e8e\u8fed\u4ee3\u8ba1\u6570\uff0c\u6ca1\u6709\u5728\u5faa\u73af\u7684\u5176\u4ed6\u5730\u65b9\u51fa\u73b0\uff0c\u8bb8\u591a\u4eba\u8fd8\u662f\u559c\u6b22\u7528\u5355\u4e2a\u5b57\u6bcd\u7684\u53d8\u91cf(i) \uff0c\u800c\u4e0d\u662f\u53e6\u5916\u53d6\u540d\u3002 <br \/>&#8211; \u53d8\u91cf\u540d\u4e2d\u4e0d\u4f7f\u7528\u4e0b\u5212\u7ebf (_) \u3002 <br \/>&#8211; \u547d\u540d\u7a7a\u95f4\u9700\u6309\u7167\u6807\u51c6\u7684\u6a21\u5f0f\u547d\u540d<\/p>\n<p>&#8230;<\/p>\n<p>\u6587\u4ef6\u540d\u8981\u548c\u7c7b\u540d\u5339\u914d <\/p>\n<p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u7c7bHelloWorld, \u76f8\u5e94\u7684\u6587\u4ef6\u540d\u5e94\u4e3a helloworld.cs (\u6216, helloworld.vb) <br \/>\u7f29\u8fdb\u548c\u95f4\u9694<br \/>\u7f29\u8fdb\u7528 TAB . \u4e0d\u7528 SPACES.\u3002 <br \/>\u6ce8\u91ca\u9700\u548c\u4ee3\u7801\u5bf9\u9f50.\u3002 <br \/>\u82b1\u62ec\u5f27 ( {} ) \u9700\u548c\u62ec\u53f7\u5916\u7684\u4ee3\u7801\u5bf9\u9f50.\u3002 <br \/>\u7528\u4e00\u4e2a\u7a7a\u884c\u6765\u5206\u5f00\u4ee3\u7801\u7684\u903b\u8f91\u5206\u7ec4\u3002. <\/p>\n<p> bool SayHello (string name) {&nbsp;&nbsp;string fullMessage = &#8220;Hello &#8221; + name;&nbsp;&nbsp;DateTime currentTime = DateTime.Now;&nbsp;&nbsp;string message = fullMessage + &#8220;, the time is : &#8221; + currentTime.ToShortTimeString();&nbsp;&nbsp;MessageBox.Show ( message );&nbsp;&nbsp;if ( &#8230; )&nbsp;&nbsp;{&nbsp;&nbsp; \/\/ Do something&nbsp;&nbsp; \/\/ &#8230;&nbsp;&nbsp; return false;&nbsp;&nbsp;}&nbsp;&nbsp;return true; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u8fd9\u6bb5\u4ee3\u7801\u770b\u8d77\u6765\u6bd4\u4e0a\u9762\u7684\u597d\uff1a: <br \/> bool SayHello ( string name ) {&nbsp;&nbsp;string fullMessage = &#8220;Hello &#8221; + name;&nbsp;&nbsp;DateTime currentTime = DateTime.Now;&nbsp;&nbsp;&nbsp;&nbsp;string message = fullMessage + &#8220;, the time is : &#8221; + currentTime.ToShortTimeString();&nbsp;&nbsp;&nbsp;&nbsp;MessageBox.Show ( message );&nbsp;&nbsp;&nbsp;&nbsp;if ( &#8230; )&nbsp;&nbsp;{&nbsp;&nbsp; \/\/ Do something&nbsp;&nbsp; \/\/ &#8230;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;return true; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u5728\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u5404\u4e2a\u65b9\u6cd5\u9700\u7528\u4e00\u7a7a\u884c\uff0c\u4e5f\u53ea\u80fd\u662f\u4e00\u884c\u5206\u5f00\u3002 <br \/>\u82b1\u62ec\u5f27\u9700\u72ec\u7acb\u4e00\u884c\uff0c\u800c\u4e0d\u8c61if, for \u7b49\u53ef\u4ee5\u8ddf\u62ec\u53f7\u5728\u540c\u4e00\u884c\u3002. <br \/>\u597d\uff1a <br \/>&nbsp;&nbsp;if ( &#8230; )&nbsp;&nbsp; {&nbsp;&nbsp; \/\/ Do something&nbsp;&nbsp;}\u4e0d\u597d\uff1a <br \/>&nbsp;&nbsp;if ( &#8230; ) {&nbsp;&nbsp; \/\/ Do something&nbsp;&nbsp;}\u5728\u6bcf\u4e2a\u8fd0\u7b97\u7b26\u548c\u62ec\u53f7\u7684\u524d\u540e\u90fd\u7a7a\u4e00\u683c\u3002. <\/p>\n<p>\u597d\uff1a <br \/>&nbsp;&nbsp;if ( showResult == true )&nbsp;&nbsp;{&nbsp;&nbsp; for ( int i = 0; i &lt; 10; i++ )&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;&nbsp; }&nbsp;&nbsp;}\u4e0d\u597d\uff1a <br \/>&nbsp;&nbsp;if(showResult==true)&nbsp;&nbsp;{&nbsp;&nbsp; for(int i= 0;i&lt;10;i++)&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;&nbsp; }&nbsp;&nbsp;}\u826f\u597d\u7684\u7f16\u7a0b\u4e60\u60ef<\/p>\n<p>\u9075\u4ece\u4ee5\u4e0b\u826f\u597d\u7684\u4e60\u60ef\u4ee5\u5199\u51fa\u597d\u7a0b\u5e8f <\/p>\n<p>\u907f\u514d\u4f7f\u7528\u5927\u6587\u4ef6\u3002\u5982\u679c\u4e00\u4e2a\u6587\u4ef6\u91cc\u7684\u4ee3\u7801\u8d85\u8fc7300\uff5e400\u884c\uff0c\u5fc5\u987b\u8003\u8651\u5c06\u4ee3\u7801\u5206\u5f00\u5230\u4e0d\u540c\u7c7b\u4e2d\u3002 <br \/>\u907f\u514d\u5199\u592a\u957f\u7684\u65b9\u6cd5\u3002\u4e00\u4e2a\u5178\u578b\u7684\u65b9\u6cd5\u4ee3\u7801\u57281\uff5e25\u884c\u4e4b\u95f4\u3002\u5982\u679c\u4e00\u4e2a\u65b9\u6cd5\u53d1\u4ee3\u7801\u8d85\u8fc725\u884c\uff0c\u5e94\u8be5\u8003\u8651\u5c06\u5176\u5206\u89e3\u4e3a\u4e0d\u540c\u7684\u65b9\u6cd5\u3002 <br \/>\u65b9\u6cd5\u540d\u9700\u80fd\u770b\u51fa\u5b83\u4f5c\u4ec0\u4e48\u3002\u522b\u4f7f\u7528\u4f1a\u5f15\u8d77\u8bef\u89e3\u7684\u540d\u5b57\u3002\u5982\u679c\u540d\u5b57\u4e00\u76ee\u4e86\u7136\uff0c\u5c31\u65e0\u9700\u7528\u6587\u6863\u6765\u89e3\u91ca\u65b9\u6cd5\u7684\u529f\u80fd\u4e86\u3002<\/p>\n<p>\u597d\uff1a <br \/> void SavePhoneNumber ( string phoneNumber ) {&nbsp;&nbsp;\/\/ Save the phone number. }<br \/>\u4e0d\u597d\uff1a <br \/> \/\/ This method will save the phone number. void SaveData ( string phoneNumber ) {&nbsp;&nbsp;\/\/ Save the phone number. }\u4e00\u4e2a\u65b9\u6cd5\u53ea\u5b8c\u6210\u4e00\u4e2a\u4efb\u52a1\u3002\u4e0d\u8981\u628a\u591a\u4e2a\u4efb\u52a1\u7ec4\u5408\u5230\u4e00\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u5373\u4f7f\u90a3\u4e9b\u4efb\u52a1\u975e\u5e38\u5c0f\u3002<\/p>\n<p>\u597d\uff1a <br \/> \/\/ Save the address. SaveAddress (&nbsp;&nbsp;address );&nbsp;&nbsp;\/\/ Send an email to the supervisor to inform that the address is updated. SendEmail ( address, email );&nbsp;&nbsp;&nbsp;&nbsp;void SaveAddress ( string address ) {&nbsp;&nbsp;\/\/ Save the address.&nbsp;&nbsp;\/\/ &#8230; }&nbsp;&nbsp;void SendEmail ( string address, string email ) {&nbsp;&nbsp;\/\/ Send an email to inform the supervisor that the address is changed.&nbsp;&nbsp;\/\/ &#8230; }<br \/>\u4e0d\u597d\uff1a <br \/> \/\/ Save address and send an email to the supervisor to inform that the address is updated. SaveAddress ( address, email ); void SaveAddress ( string address, string email ) {&nbsp;&nbsp;\/\/ Job 1.&nbsp;&nbsp;\/\/ Save the address.&nbsp;&nbsp;\/\/ &#8230;&nbsp;&nbsp;\/\/ Job 2.&nbsp;&nbsp;\/\/ Send an email to inform the supervisor that the address is changed.&nbsp;&nbsp;\/\/ &#8230; }\u4f7f\u7528C# \u6216 VB.NET\u7684\u7279\u6709\u7c7b\u578b\uff0c\u800c\u4e0d\u662fSystem\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u522b\u540d\u7c7b\u578b\u3002<\/p>\n<p>\u597d\uff1a <br \/> int age; string name; object contactInfo;<br \/>\u4e0d\u597d\uff1a <br \/> Int16 age; String name; Object contactInfo; \u522b\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u56fa\u5b9a\u6570\u503c\uff0c\u7528\u5e38\u91cf\u4ee3\u66ff\u3002 <br \/>\u522b\u7528\u5b57\u7b26\u4e32\u5e38\u6570\u3002\u7528\u8d44\u6e90\u6587\u4ef6\u3002 <br \/>\u907f\u514d\u4f7f\u7528\u5f88\u591a\u6210\u5458\u53d8\u91cf\u3002\u58f0\u660e\u5c40\u90e8\u53d8\u91cf\uff0c\u5e76\u4f20\u9012\u7ed9\u65b9\u6cd5\u3002\u4e0d\u8981\u5728\u65b9\u6cd5\u95f4\u5171\u4eab\u6210\u5458\u53d8\u91cf\u3002\u5982\u679c\u5728\u51e0\u4e2a\u65b9\u6cd5\u95f4\u5171\u4eab\u4e00\u4e2a\u6210\u5458\u53d8\u91cf\uff0c\u90a3\u5c31\u5f88\u96be\u77e5\u9053\u662f\u54ea\u4e2a\u65b9\u6cd5\u5728\u4ec0\u4e48\u65f6\u5019\u4fee\u6539\u4e86\u5b83\u7684\u503c\u3002 <br \/>\u5fc5\u8981\u65f6\u4f7f\u7528enum \u3002\u522b\u7528\u6570\u5b57\u6216\u5b57\u7b26\u4e32\u6765\u6307\u793a\u79bb\u6563\u503c\u3002 <br \/>\u597d\uff1a <br \/> enum MailType {&nbsp;&nbsp;Html,&nbsp;&nbsp;PlainText,&nbsp;&nbsp;Attachment } void SendMail (string message, MailType mailType) {&nbsp;&nbsp;switch ( mailType )&nbsp;&nbsp;{&nbsp;&nbsp; case MailType.Html:&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Do something&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp; case MailType.PlainText:&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Do something&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp; case MailType.Attachment:&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Do something&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp; default:&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Do something&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp;} }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>\u4e0d\u597d\uff1a <br \/> void SendMail (string message, string mailType) {&nbsp;&nbsp;switch ( mailType )&nbsp;&nbsp;{&nbsp;&nbsp; case &#8220;Html&#8221;:&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Do something&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp; case &#8220;PlainText&#8221;:&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Do something&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp; case &#8220;Attachment&#8221;:&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Do something&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp; default:&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Do something&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp;} }\u522b\u628a\u6210\u5458\u53d8\u91cf\u58f0\u660e\u4e3a public \u6216 protected\u3002\u90fd\u58f0\u660e\u4e3a private \u800c\u4f7f\u7528 public\/protected \u7684Properties. <br \/>\u4e0d\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5177\u4f53\u7684\u8def\u5f84\u548c\u9a71\u52a8\u5668\u540d\u3002 \u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\uff0c\u5e76\u4f7f\u8def\u5f84\u53ef\u7f16\u7a0b\u3002 <br \/>\u6c38\u8fdc\u522b\u8bbe\u60f3\u4f60\u7684\u4ee3\u7801\u662f\u5728\u201cC:\u201d\u76d8\u8fd0\u884c\u3002\u4f60\u4e0d\u4f1a\u77e5\u9053\uff0c\u4e00\u4e9b\u7528\u6237\u5728\u7f51\u7edc\u6216\u201cZ:\u201d\u76d8\u8fd0\u884c\u7a0b\u5e8f\u3002 <br \/>\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u4f5c\u4e9b\u201c\u81ea\u68c0\u201d\u5e76\u786e\u4fdd\u6240\u9700\u6587\u4ef6\u548c\u9644\u4ef6\u5728\u6307\u5b9a\u7684\u4f4d\u7f6e\u3002\u5fc5\u8981\u65f6\u68c0\u67e5\u6570\u636e\u5e93\u8fde\u63a5\u3002\u51fa\u73b0\u4efb\u4f55\u95ee\u9898\u7ed9\u7528\u6237\u4e00\u4e2a\u53cb\u597d\u7684\u63d0\u793a\u3002 <br \/>\u5982\u679c\u9700\u8981\u7684\u914d\u7f6e\u6587\u4ef6\u627e\u4e0d\u5230\uff0c\u5e94\u7528\u7a0b\u5e8f\u9700\u80fd\u81ea\u5df1\u521b\u5efa\u4f7f\u7528\u9ed8\u8ba4\u503c\u7684\u4e00\u4efd\u3002 <br \/>\u5982\u679c\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u53d1\u73b0\u9519\u8bef\u503c\uff0c\u5e94\u7528\u7a0b\u5e8f\u8981\u629b\u51fa\u9519\u8bef\uff0c\u7ed9\u51fa\u63d0\u793a\u6d88\u606f\u544a\u8bc9\u7528\u6237\u6b63\u786e\u503c\u3002 <br \/>\u9519\u8bef\u6d88\u606f\u9700\u80fd\u5e2e\u52a9\u7528\u6237\u89e3\u51b3\u95ee\u9898\u3002\u6c38\u8fdc\u522b\u7528\u8c61&#8221;\u5e94\u7528\u7a0b\u5e8f\u51fa\u9519&#8221;, &#8220;\u53d1\u73b0\u4e00\u4e2a\u9519\u8bef&#8221; \u7b49\u9519\u8bef\u6d88\u606f\u3002\u800c\u5e94\u7ed9\u51fa\u8c61 &#8220;\u66f4\u65b0\u6570\u636e\u5e93\u5931\u8d25\u3002\u8bf7\u786e\u4fdd\u767b\u9646id\u548c\u5bc6\u7801\u6b63\u786e\u3002&#8221; \u7684\u5177\u4f53\u6d88\u606f\u3002&nbsp;&nbsp; <br \/>\u663e\u793a\u9519\u8bef\u6d88\u606f\u65f6\uff0c\u9664\u4e86\u8bf4\u54ea\u91cc\u9519\u4e86\uff0c\u8fd8\u5e94\u63d0\u793a\u7528\u6237\u5982\u4f55\u89e3\u51b3\u95ee\u9898\u3002\u4e0d\u8981\u7528 \u8c61 &#8220;\u66f4\u65b0\u6570\u636e\u5e93\u5931\u8d25\u3002&#8221;\u8fd9\u6837\u7684\uff0c\u8981\u63d0\u793a\u7528\u6237\u600e\u4e48\u505a\uff1a&#8221;\u66f4\u65b0\u6570\u636e\u5e93\u5931\u8d25\u3002\u8bf7\u786e\u4fdd\u767b\u9646id\u548c\u5bc6\u7801\u6b63\u786e\u3002&#8221; <br \/>\u663e\u793a\u7ed9\u7528\u6237\u7684\u6d88\u606f\u8981\u7b80\u77ed\u800c\u53cb\u597d\u3002\u4f46\u8981\u628a\u6240\u6709\u53ef\u80fd\u7684\u4fe1\u606f\u90fd\u8bb0\u5f55\u4e0b\u6765\uff0c\u4ee5\u52a9\u8bca\u65ad\u95ee\u9898\u3002 <br \/>\u6ce8\u91ca<br \/>\u522b\u6bcf\u884c\u4ee3\u7801\uff0c\u6bcf\u4e2a\u58f0\u660e\u7684\u53d8\u91cf\u90fd\u505a\u6ce8\u91ca\u3002 <br \/>\u5728\u9700\u8981\u7684\u5730\u65b9\u6ce8\u91ca\u3002\u53ef\u8bfb\u6027\u5f3a\u7684\u4ee3\u7801\u9700\u8981\u5f88\u5c11\u7684\u6ce8\u91ca\u3002\u5982\u679c\u6240\u6709\u7684\u53d8\u91cf\u548c\u65b9\u6cd5\u7684\u547d\u540d\u90fd\u5f88\u6709\u610f\u4e49\uff0c\u4f1a\u4f7f\u4ee3\u7801\u53ef\u8bfb\u6027\u5f88\u5f3a\u5e76\u65e0\u9700\u592a\u591a\u6ce8\u91ca\u3002 <br \/>\u884c\u6570\u4e0d\u591a\u7684\u6ce8\u91ca\u4f1a\u4f7f\u4ee3\u7801\u770b\u8d77\u6765\u4f18\u96c5\u3002\u4f46\u5982\u679c\u4ee3\u7801\u4e0d\u6e05\u6670\uff0c\u53ef\u8bfb\u6027\u5dee\uff0c\u90a3\u5c31\u7cdf\u7cd5\u3002 <br \/>\u5982\u679c\u5e94\u4e3a\u67d0\u79cd\u539f\u56e0\u4f7f\u7528\u4e86\u590d\u6742\u8270\u6da9\u7684\u539f\u7406\uff0c\u4e3a\u7a0b\u5e8f\u914d\u5907\u826f\u597d\u7684\u6587\u6863\u548c\u91cd\u5206\u7684\u6ce8\u91ca\u3002 <br \/>\u5bf9\u4e00\u4e2a\u6570\u503c\u53d8\u91cf\u91c7\u7528\u4e0d\u662f0,-1\u7b49\u7684\u6570\u503c\u521d\u59cb\u5316\uff0c\u7ed9\u51fa\u9009\u62e9\u8be5\u503c\u7684\u7406\u7531\u3002 <br \/>\u7b80\u8a00\u4e4b\uff0c\u8981\u5199\u6e05\u6670\uff0c\u53ef\u8bfb\u7684\u4ee3\u7801\u4ee5\u81f4\u65e0\u987b\u4ec0\u4e48\u6ce8\u91ca\u5c31\u80fd\u7406\u89e3\u3002 <br \/>\u5bf9\u6ce8\u91ca\u505a\u62fc\u5199\u68c0\u67e5\uff0c\u4fdd\u8bc1\u8bed\u6cd5\u548c\u6807\u70b9\u7b26\u53f7\u7684\u6b63\u786e\u4f7f\u7528\u3002<\/p>\n<p>\u5f02\u5e38\u5904\u7406<br \/>\u4e0d\u8981\u201c\u6355\u6349\u4e86\u5f02\u5e38\u5374\u4ec0\u4e48\u4e5f\u4e0d\u505a\u201c\u3002\u5982\u679c\u9690\u85cf\u4e86\u4e00\u4e2a\u5f02\u5e38\uff0c\u4f60\u5c06\u6c38\u8fdc\u4e0d\u77e5\u9053\u5f02\u5e38\u5230\u5e95\u53d1\u751f\u4e86\u6ca1\u6709\u3002 <br \/>\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u7ed9\u51fa\u53cb\u597d\u7684\u6d88\u606f\u7ed9\u7528\u6237\uff0c\u4f46\u8981\u7cbe\u786e\u8bb0\u5f55\u9519\u8bef\u7684\u6240\u6709\u53ef\u80fd\u7ec6\u8282\uff0c\u5305\u62ec\u53d1\u751f\u7684\u65f6\u95f4\uff0c\u548c\u76f8\u5173\u65b9\u6cd5\uff0c\u7c7b\u540d\u7b49\u3002 <br \/>\u53ea\u6355\u6349\u7279\u5b9a\u7684\u5f02\u5e38\uff0c\u800c\u4e0d\u662f\u4e00\u822c\u7684\u5f02\u5e38\u3002<\/p>\n<p>\u597d\uff1a <br \/> void ReadFromFile ( string fileName ) {&nbsp;&nbsp;try&nbsp;&nbsp;{&nbsp;&nbsp; \/\/ read from file.&nbsp;&nbsp;}&nbsp;&nbsp;catch (FileIOException ex)&nbsp;&nbsp;{&nbsp;&nbsp; \/\/ log error.&nbsp;&nbsp; \/\/&nbsp;&nbsp;re-throw exception depending on your case.&nbsp;&nbsp; throw;&nbsp;&nbsp;} }\u4e0d\u597d\uff1a <br \/> void ReadFromFile ( string fileName ) {&nbsp;&nbsp;try&nbsp;&nbsp;{&nbsp;&nbsp; \/\/ read from file.&nbsp;&nbsp;}&nbsp;&nbsp;catch (Exception ex)&nbsp;&nbsp; {&nbsp;&nbsp; \/\/ Catching general exception is bad&#8230; we will never know whether it&nbsp;&nbsp; \/\/ was a file error or some other error.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Here you are hiding an exception.&nbsp;&nbsp;&nbsp;&nbsp;\/\/ In this case no one will ever know that an exception happened.&nbsp;&nbsp; return &#8220;&#8221;;&nbsp;&nbsp;&nbsp;&nbsp;} }\u4e0d\u5fc5\u5728\u6240\u6709\u65b9\u6cd5\u4e2d\u6355\u6349\u4e00\u822c\u5f02\u5e38\u3002\u4e0d\u7ba1\u5b83\uff0c\u8ba9\u7a0b\u5e8f\u5d29\u6e83\u3002\u8fd9\u5c06\u5e2e\u52a9\u4f60\u5728\u5f00\u53d1\u5468\u671f\u53d1\u73b0\u5927\u591a\u6570\u7684\u9519\u8bef\u3002 <br \/>\u4f60\u53ef\u4ee5\u7528\u5e94\u7528\u7a0b\u5e8f\u7ea7\uff08\u7ebf\u7a0b\u7ea7\uff09\u9519\u8bef\u5904\u7406\u5668\u5904\u7406\u6240\u6709\u4e00\u822c\u7684\u5f02\u5e38\u3002\u9047\u5230\u201d\u4ee5\u5916\u7684\u4e00\u822c\u6027\u9519\u8bef\u201c\u65f6\uff0c\u6b64\u9519\u8bef\u5904\u7406\u5668\u5e94\u8be5\u6355\u6349\u5f02\u5e38\uff0c\u7ed9\u7528\u6237\u63d0\u793a\u6d88\u606f\uff0c\u5728\u5e94\u7528\u7a0b\u5e8f\u5173\u95ed\u6216 \u7528\u6237\u9009\u62e9\u201d\u5ffd\u7565\u5e76\u7ee7\u7eed\u201c\u4e4b\u524d\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u3002 <br \/>\u4e0d\u5fc5\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u7528try-catch\u3002\u5f53\u7279\u5b9a\u7684\u5f02\u5e38\u53ef\u80fd\u53d1\u751f\u65f6\u624d\u4f7f\u7528\u3002\u6bd4\u5982\uff0c\u5f53\u4f60\u5199\u6587\u4ef6\u65f6\uff0c\u5904\u7406\u5f02\u5e38FileIOException. <br \/>\u522b\u5199\u592a\u5927\u7684 try-catch \u6a21\u5757\u3002\u5982\u679c\u9700\u8981\uff0c\u4e3a\u6bcf\u4e2a\u6267\u884c\u7684\u4efb\u52a1\u7f16\u5199\u5355\u72ec\u7684 try-catch \u6a21\u5757\u3002 \u8fd9\u5c06\u5e2e\u4f60\u627e\u51fa\u54ea\u4e00\u6bb5\u4ee3\u7801\u4ea7\u751f\u5f02\u5e38\uff0c\u5e76\u7ed9\u7528\u6237\u53d1\u51fa\u7279\u5b9a\u7684\u9519\u8bef\u6d88\u606f <br \/>\u5982\u679c\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\uff0c\u53ef\u4ee5\u7f16\u5199\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0d\u5e94\u4ece\u57fa\u7c7bSystemException\u6d3e\u751f\uff0c\u800c\u8981\u7ee7\u627f\u4e8e. IApplicationException\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u64b0\u6587\uff1aDotNetSpider.com\uff08http:\/\/www.dot&#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-79","post","type-post","status-publish","format-standard","hentry","category-NET"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/79","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=79"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/79\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}