{"id":63,"date":"2008-07-09T06:37:57","date_gmt":"2008-07-09T06:37:57","guid":{"rendered":"http:\/\/tw.newtonstudio.com\/?p=63"},"modified":"2008-07-09T06:51:24","modified_gmt":"2008-07-09T06:51:24","slug":"visual-cmethod%e5%82%b3%e5%85%a5%e5%8f%83%e6%95%b8%e5%90%8c%e6%99%82%e5%8f%af%e4%bb%a5%e6%98%af%e9%99%a3%e5%88%97%e8%88%87integer","status":"publish","type":"post","link":"http:\/\/tw.newtonstudio.com\/?p=63","title":{"rendered":"[Visual C#]Params\u7684\u61c9\u7528: Method\u50b3\u5165\u53c3\u6578\u540c\u6642\u53ef\u4ee5\u662f\u9663\u5217\u8207Integer?"},"content":{"rendered":"<p>\u7576\u6211\u5011\u5728\u64b0\u5beb\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u7684\u6642\u5019, \u96e3\u514d\u6703\u9700\u8981\u5c07\u53c3\u6578\u50b3\u5165Method\u88e1\u9762, \u82e5\u50b3\u5165\u7684\u53c3\u6578\u662f\u9663\u5217, \u683c\u5f0f\u5982\u4e0b:<\/p>\n<blockquote><p>static void Main(string[] args)<br \/>\n{<\/p>\n<p>int[] a = new int[2]{5,7};<\/p>\n<p>Util util = new Util();<\/p>\n<p>Console.WriteLine(util.Min(a));<\/p>\n<p>}<\/p>\n<p>class Util<br \/>\n{<\/p>\n<p>public int Min(int[] param)<br \/>\n{<br \/>\nif (param == null || param.Length == 0)<br \/>\n{<br \/>\nthrow new ArgumentException(&#8220;Util.Min: not enough arguments&#8221;);<br \/>\n}<\/p>\n<p>int currentmin = param[0];<\/p>\n<p>foreach (int i in param)<br \/>\n{<br \/>\nif (i &lt; currentmin)<br \/>\n{<br \/>\ncurrentmin = i;<br \/>\n}<br \/>\n}<\/p>\n<p>return currentmin;<br \/>\n}<\/p>\n<p>}<\/p><\/blockquote>\n<p>\u9019\u7a2e\u5beb\u6cd5\u61c9\u8a72\u5f88\u76f4\u89c0\u5427, \u4f46\u5982\u679c\u50b3\u5165\u7684\u53c3\u6578\u662fInteger\u8c48\u4e0d\u662f\u51fa\u73feError? \u8981\u600e\u9ebc\u89e3\u6c7a\u5462? \u5176\u5be6\u8981\u89e3\u6c7a\u9019\u500b\u554f\u984c\u975e\u5e38\u7c21\u55ae, \u5c31\u662f\u5728Method\u7684\u53c3\u6578\u90e8\u5206\u52a0\u5165 &#8220;params&#8221;\u9019\u500b\u95dc\u9375\u5b57, \u5beb\u6cd5\u5982\u4e0b:<\/p>\n<blockquote><p>class Util<br \/>\n{<\/p>\n<p>public int Min(<strong><span style=\"color: #ff0000;\">params<\/span><\/strong> int[] param)<br \/>\n{<br \/>\nif (param == null || param.Length == 0)<br \/>\n{<br \/>\nthrow new ArgumentException(&#8220;Util.Min: not enough arguments&#8221;);<br \/>\n}<\/p>\n<p>int currentmin = param[0];<\/p>\n<p>foreach (int i in param)<br \/>\n{<br \/>\nif (i &lt; currentmin)<br \/>\n{<br \/>\ncurrentmin = i;<br \/>\n}<br \/>\n}<\/p>\n<p>return currentmin;<br \/>\n}<\/p>\n<p>}<\/p><\/blockquote>\n<p>\u9019\u6a23\u4e00\u4f86\u50b3\u5165\u7684\u53c3\u6578\u6703\u7d93\u904ecompiler\u505a\u4ee5\u4e0b\u7684\u52d5\u4f5c:<\/p>\n<blockquote><p>int min = Util.Min(first, second);<br \/>\nint[] array = new int[2];<br \/>\narray[0] = first;<br \/>\narray[1] = second;<br \/>\nint min = Util.Min(array);<\/p><\/blockquote>\n<p>\u4f60\u7684\u4e3b\u7a0b\u5f0f\u5c31\u53ef\u4ee5\u5f48\u6027\u50b3\u5165\u53c3\u6578, \u5920\u65b9\u4fbf\u5427?<\/p>\n<blockquote><p>static void Main(string[] args)<br \/>\n{<\/p>\n<p>int[] a = new int[2]{5,7};<\/p>\n<p>Util util = new Util();<\/p>\n<p><strong><span style=\"color: #ff0000;\">Console.WriteLine(util.Min(a));\/\/\u53ef\u4ee5\u50b3\u5165\u9663\u5217\u578b\u614b\u7684\u8b8a\u6578<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #ff0000;\">int b = util.Min(2, 10); \/\/\u540c\u6642\u53ef\u4ee5\u50b3\u5165\u591a\u500bInteger<\/span><\/strong><\/p>\n<p>Console.WriteLine(b);<\/p>\n<p>}<\/p><\/blockquote>\n<p>\u4f7f\u7528params\u6709\u5e7e\u500b\u6ce8\u610f\u4e8b\u9805:<\/p>\n<li>\u9663\u5217\u4e0d\u80fd\u662fmultidimensional<\/li>\n<li>\u4f7f\u7528params\u7684method\u4e0d\u80fdOverloading (\u591a\u8f09)<\/li>\n<li>\u4f7f\u7528params\u7684method\u4e0d\u80fd\u662fref \u6216\u9019 out<\/li>\n<li>method\u50b3\u5165\u7684\u53c3\u6578\u53ea\u80fd\u6709\u4e00\u500bparams array<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u7576\u6211\u5011\u5728\u64b0\u5beb\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u7684\u6642\u5019, \u96e3\u514d\u6703\u9700\u8981\u5c07\u53c3\u6578\u50b3\u5165Method\u88e1\u9762, \u82e5\u50b3\u5165\u7684\u53c3\u6578\u662f\u9663\u5217, \u683c\u5f0f\u5982\u4e0b: s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[40,39],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-net-framework-aspx","tag-c","tag-params"],"_links":{"self":[{"href":"http:\/\/tw.newtonstudio.com\/index.php?rest_route=\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/tw.newtonstudio.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/tw.newtonstudio.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/tw.newtonstudio.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/tw.newtonstudio.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=63"}],"version-history":[{"count":0,"href":"http:\/\/tw.newtonstudio.com\/index.php?rest_route=\/wp\/v2\/posts\/63\/revisions"}],"wp:attachment":[{"href":"http:\/\/tw.newtonstudio.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/tw.newtonstudio.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/tw.newtonstudio.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}