{"id":2087,"date":"2021-09-22T05:35:22","date_gmt":"2021-09-21T21:35:22","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=2087"},"modified":"2022-02-16T21:45:05","modified_gmt":"2022-02-16T13:45:05","slug":"method-property","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2021\/09\/22\/method-property\/","title":{"rendered":"D-23 \u65b9\u6cd5 method ? property"},"content":{"rendered":"<h1>\u8cc7\u6599\u7d50\u69cb\u4e4b\u5f8c\u5462<\/h1>\n<p>\u5728\u6628\u5929\u5927\u982d\u8ddf\u5c0f\u5149\u4ecb\u7d39\u4e86\u8cc7\u6599\u7d50\u69cb\u7684\u5167\u5bb9\uff0c\u4f46\u662f\u6709\u4e9b\u6771\u897f\u5927\u982d\u6c92\u6709\u8ddf\u5c0f\u5149\u7279\u5225\u89e3\u91cb\uff0c\u4f8b\u5982\u4ecb\u9762\u7269\u4ef6\u4e4b\u985e\u7684\uff0c\u9084\u6709\u5728\u4f8b\u5b50\u88cf\u9762\u6709<code>Count<\/code>\u8ddf<code>Add<\/code>\u9084\u6709<code>Remove<\/code>\u4e4b\u985e\u7684\u95dc\u9375\u5b57\u90fd\u6c92\u6709\u8aaa\u660e\uff0c\u9019\u8b93\u5c0f\u5149\u6574\u665a\u4e0d\u80fd\u8aaa\u5728\u601d\u8003\u90a3\u662f\u751a\u9ebc\u6771\u897f\uff0c\u6240\u4ee5\u4eca\u5929\u5c0f\u5149\u6703\u6709\u751a\u9ebc\u6536\u7a6b\u5462\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u65b9\u6cd5 method<\/h2>\n<p>\u300c\u524d\u8f29\uff0c\u7a76\u7adf\u90a3\u500b<code>.Add<\/code>\u8ddf<code>.Count<\/code>\u662f\u4ec0\u9ebc\u6771\u897f\u554a\uff0c\u70ba\u4ec0\u9ebc<code>int<\/code>\u6c92\u6709\u4f46\u662f<code>List<\/code>\u6709\u554a\uff1f\u300d<br \/>\n\u4e00\u5927\u65e9\u5c0f\u5149\u5e36\u8457\u9ed1\u773c\u5708\u6709\u6c23\u7121\u529b\u7684\u554f\u8457\u5927\u982d\u554f\u984c\uff0c\u800c\u5927\u982d\u4ecd\u820a\u4f4e\u8457\u982d\u5728\u958b\u767c\uff0c\u7b49\u5230\u904e\u5e7e\u79d2\u4e4b\u5f8c\u5927\u982d\u62ac\u8d77\u982d\u4f86\u6e96\u5099\u56de\u8986\u5c0f\u5149\u6642\u7a81\u7136\u88ab\u5c0f\u5149\u7684\u6a23\u5b50\u4e0b\u4e86\u4e00\u5927\u8df3\u3002<br \/>\n\u300c\u4f60\u662f\u600e\u9ebc\u6a23\u554a\uff0c\u8003\u8a66\u7b49\u653e\u699c\u9084\u662f\u8ddf\u5973\u53cb\u7d04\u8981\u8ac7\u5224\u6240\u4ee5\u6574\u665a\u7dca\u5f35\u5230\u7761\u4e0d\u8457\u89ba\u55ce\uff1f\u300d<br \/>\n\u9019\u6642\u5c0f\u5149\u9732\u51fa\u82e6\u7b11\u4e26\u4e14\u6293\u6293\u982d\u7684\u56de\u7b54\u8aaa\u3002<br \/>\n\u300c\u6c92\u6709\u5566\uff0c\u56e0\u70ba\u592a\u597d\u5947\u4e86\u6240\u4ee5\u6c92\u7761\u597d\uff0c\u7a76\u7adf\u90a3\u662f\u4ec0\u9ebc\u6771\u897f\u554a\uff1f\u300d<br \/>\n\u300c\u54c8\u54c8\u54c8\uff0c\u597d\u5427\u597d\u5427\u8b93\u6211\u4f86\u89e3\u9664\u4f60\u7684\u7591\u60d1\u5427\uff0c\u90a3\u5c31\u662f\u65b9\u6cd5\u8ddf\u5c6c\u6027\uff0c\u63a5\u4e0b\u4f86\u6211\u518d\u4f86\u8aaa\u660e\u9019\u500b\u6771\u897f\u5427\u3002\u300d<\/p>\n<h3>\u65b9\u6cd5 method<\/h3>\n<p>\u6240\u8b02\u7684\u65b9\u6cd5\u5c31\u662f\u628a\u4e00\u5806\u7a0b\u5f0f\u6574\u5408\u5728\u4e00\u8d77\uff0c\u8a73\u7d30\u7684\u8aaa\u660e\u8acb\u770b\u770b<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/csharp\/programming-guide\/classes-and-structs\/methods\" title=\"msdn \u65b9\u6cd5\" rel=\"nofollow noopener\" target=\"_blank\">msdn \u65b9\u6cd5<\/a>\uff0c\u4e0d\u904e\u7c21\u55ae\u8b1b\u5c31\u662f\u8b93\u4f60\u53ef\u4ee5\u770b\u5230\u65b9\u6cd5\u7684\u540d\u5b50\u5c31\u77e5\u9053\u88e1\u9762\u5728\u505a\u751a\u9ebc\u7684\u6982\u5ff5\uff0c\u6240\u4ee5\u53c8\u53ef\u4ee5\u4f9d\u64da<a href=\"#\u5b58\u53d6\u4fee\u98fe\u8a5e\">\u5b58\u53d6\u4fee\u98fe\u8a5e<\/a>\u4f86\u8a2d\u5b9a\u54ea\u4e9b\u662f\u4f9b\u5916\u90e8\u4f7f\u7528\u7684\u65b9\u6cd5\uff0c\u54ea\u4e9b\u662f\u985e\u5225\u81ea\u5df1\u4f7f\u7528\u7684\u65b9\u6cd5\u3002\u6240\u4ee5\u958b\u767c\u5feb\u7684\u5de5\u7a0b\u5e2b\u5728\u958b\u767c\u4e00\u500b\u65b0\u7684\u9700\u6c42\u6642\u5c31\u6703\u5148\u770b\u4e00\u4e0b\u4e4b\u524d\u6709\u6c92\u6709\u5beb\u904e\u985e\u4f3c\u7684\u65b9\u6cd5\uff0c\u6216\u8005\u53ef\u4ee5\u4fee\u6539\u70ba\u901a\u7528\u7684\u65b9\u6cd5\u3002\u8aaa\u4e86\u90a3\u9ebc\u591a\u65b9\u6cd5\u7684\u597d\u8655\u5c31\u4f86\u770b\u4e00\u4e0b\u8981\u600e\u9ebc\u64b0\u5beb\u4e00\u500b\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-cs\">using System;\n\npublic class Program\n{\n        \/\/ \u7121\u8f38\u5165\uff0c\u7121\u8fd4\u56de\u7684\u65b9\u6cd5\n    public static void PrintSomething()\n    {\n        Console.WriteLine(&quot;Something&quot;);\n    }\n\n        \/\/ \u8f38\u5165int\u7684\u65b9\u6cd5\n    public static void PrintSomethingParam(int param)\n    {\n        Console.WriteLine(param);\n    }\n\n        \/\/ \u8f38\u5165string\u7684\u65b9\u6cd5\n    public static void PrintSomethingParam(string param)\n    {\n        Console.WriteLine(param);\n    }\n\n        \/\/ \u8fd4\u56deint\u7684\u65b9\u6cd5\n    public static int GetSomething()\n    {\n        return 10;\n    }\n\n        \/\/ c# \u7a0b\u5f0f\u9032\u5165\u9ede\n    public static void Main()\n    {\n        PrintSomething();\n        PrintSomethingParam(10);\n        PrintSomethingParam(&quot;11&quot;);\n        var param = GetSomething();\n        Console.WriteLine(param);\n    }\n}<\/code><\/pre>\n<p>\u4e00\u6b21\u628a\u65b9\u6cd5\u7684\u5ba3\u544a\u53ca\u5be6\u4f5c\u7684\u5f0f\u5b50\u8ddf\u5927\u5bb6\u8aaa\u660e\uff0c\u4f46\u662f\u9084\u6709\u4e9b\u65b9\u6cd5\u7684\u7279\u6027\u8981\u5f85\u4e4b\u5f8c\u4ecb\u7d39\u7269\u4ef6\u6642\u518d\u8ddf\u5927\u5bb6\u8aaa\u660e\uff0c\u6240\u4ee5\u5c0d\u65bc\u65b9\u6cd5\u7684\u4ecb\u7d39\u5c31\u5230\u9019\u908a\u544a\u4e00\u6bb5\u843d\u3002<\/p>\n<h3>\u5c6c\u6027 property<\/h3>\n<p>\u5728\u4ecb\u7d39<code>property<\/code>\u4e4b\u524d\u8b80\u8005\u5011\u53ef\u4ee5\u5148\u770b\u770b<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/csharp\/programming-guide\/classes-and-structs\/properties\" title=\"msdn \u5c6c\u6027\" rel=\"nofollow noopener\" target=\"_blank\">msdn \u5c6c\u6027<\/a>\u600e\u9ebc\u8aaa\uff0c\u4e0d\u904e\u6c92\u770b\u4e5f\u6c92\u95dc\u4fc2\uff0c\u7c21\u55ae\u8aaa\u9054\u5230<a href=\"https:\/\/zh.wikipedia.org\/wiki\/%E5%BC%80%E9%97%AD%E5%8E%9F%E5%88%99\" title=\"wiki OCP The Open\/Closed Principle\" rel=\"nofollow noopener\" target=\"_blank\">OCP The Open\/Closed Principle<\/a>\u539f\u5247\u8981\u505a\u4e9b\u624b\u6bb5\u8981\u5c07\u8b8a\u6578\u7d66\u5c01\u88dd\u8d77\u4f86\uff0c\u4e0d\u8b93\u5916\u90e8\u4efb\u610f\u5b58\u53d6\u7269\u4ef6\u7684\u8b8a\u6578\u4ee5\u53ca\u4e0d\u8b93\u5916\u90e8\u66f4\u6539\u53d6\u5f97\u8b8a\u6578\u7684\u908f\u8f2f\u800c\u7522\u751f\u7684\u529f\u80fd\uff0c\u901a\u5e38\u6703\u8ddf<a href=\"#\u5b58\u53d6\u4fee\u98fe\u8a5e\">\u5b58\u53d6\u4fee\u98fe\u8a5e<\/a>\u4e00\u8d77\u8aaa\u660e\uff0c\u4e0d\u904e\u9019\u500b\u5f85\u4e4b\u5f8c\u518d\u4f86\u8aaa\u660e\uff0c\u6240\u4ee5\u7c21\u55ae\u8aaa\u5c31\u662f\u53d6\u7684\u7269\u4ef6\u7684\u67d0\u500b\u8b8a\u6578\u7684\u624b\u6bb5\u53eb\u505a\u5c6c\u6027\uff0c\u800c\u5176\u5ba3\u544a\u4ee5\u53ca\u5be6\u4f5c\u7684\u65b9\u5f0f\u5982\u4e0b\u3002<\/p>\n<pre><code class=\"language-csharp\">using System;\n\npublic class Program\n{\n    public static int Count { get;set; } = 10;\n    public static void Main()\n    {\n        Console.WriteLine(Count);\n    }\n}<\/code><\/pre>\n<p>\u5176\u4e2d<code>Count<\/code>\u5c31\u662f<code>class Program<\/code>\u7684\u4e00\u500b\u5c6c\u6027\uff0c\u800c\u5b83\u7684\u529f\u7528\u5c31\u7b49\u65bc\u662f\u900f\u904e\u9019\u5c6c\u6027\u4f86\u5b58\u53d6\u4e00\u500b\u672a\u88ab\u5ba3\u544a\u7684<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/csharp\/programming-guide\/classes-and-structs\/fields\" title=\"msdn \u6b04\u4f4d\" rel=\"nofollow noopener\" target=\"_blank\">msdn \u6b04\u4f4d<\/a>\uff0c\u66f4\u7c21\u55ae\u8aaa\u660e\u5c31\u662f\u5b58\u53d6\u9019<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/csharp\/programming-guide\/classes-and-structs\/fields\" title=\"msdn \u6b04\u4f4d\" rel=\"nofollow noopener\" target=\"_blank\">msdn \u6b04\u4f4d<\/a>\u7684\u4e00\u7a2e\u65b9\u6cd5\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u4f8b\u5b50\u8ddf\u4e0a\u8ff0\u4f8b\u5b50\u662f\u7b49\u50f9\u7684\u3002<\/p>\n<pre><code class=\"language-csharp\">using System;\n\npublic class Program\n{\n    private static int _count = 10;\n    public static int Count \n    { \n        get\n        {\n            return _count;\n        }\n        set\n        {\n            _count = value;\n        }\n    }\n    public static void Main()\n    {\n        Console.WriteLine(Count);\n    }\n}<\/code><\/pre>\n<h3>\u5b58\u53d6\u4fee\u98fe\u8a5e<\/h3>\n<p>\u95dc\u65bc\u5b58\u53d6\u4fee\u98fe\u8a5e\u53ef\u4ee5\u770b\u4e00\u4e0b<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/csharp\/programming-guide\/classes-and-structs\/access-modifiers\" title=\"msdn \u5b58\u53d6\u4fee\u98fe\u8a5e\" rel=\"nofollow noopener\" target=\"_blank\">msdn \u5b58\u53d6\u4fee\u98fe\u8a5e<\/a>\u7684\u8aaa\u660e\uff0c\u7c21\u55ae\u8aaa\u660e\u5c31\u662f\u9019\u500b\u65b9\u6cd5\u3001\u7269\u4ef6\u3001\u5c6c\u6027\u53ef\u4e0d\u53ef\u4ee5\u4f7f\u7528\u7684\u4fee\u98fe\u8a5e\uff0c\u4f8b\u5982<code>private<\/code>\u662f\u53ea\u6709\u7269\u4ef6\u672c\u8eab\u53ef\u4ee5\u4f7f\u7528\uff0c\u51fa\u4e86\u9019\u500b\u7269\u4ef6\u5916\u9762\u7684\u4efb\u4f55\u4eba\u90fd\u4e0d\u80fd\u5b58\u53d6\u3002<\/p>\n<h2>\u5f8c\u8a18<\/h2>\n<p>\u300c\u524d\u8f29\uff0c\u7d93\u904e\u4f60\u7684\u89e3\u91cb\u7d42\u65bc\u89e3\u6c7a\u6211\u4e00\u6574\u665a\u7684\u56f0\u60d1\u3002\u300d<br \/>\n\u807d\u5b8c\u89e3\u91cb\u5f8c\u5c0f\u5149\u8c41\u7136\u958b\u6717\u7684\u6a23\u5b50\uff0c\u4e0d\u904e\u6c92\u7dad\u6301\u591a\u4e45\u53c8\u9677\u5165\u6c89\u601d\u7684\u72c0\u6cc1\u3002<br \/>\n\u300c\u600e\u9ebc\u5566\uff0c\u9084\u6709\u751a\u9ebc\u4e0d\u660e\u767d\u7684\u5462?\u300d<br \/>\n\u770b\u5230\u5c0f\u5149\u7684\u6c89\u601d\uff0c\u5927\u982d\u5fcd\u4e0d\u4f4f\u9019\u6a23\u5b50\u554f\u3002<br \/>\n\u300c\u6c92\u6709\u5566\uff0c\u9023\u7e8c\u5169\u5929\u770b\u5230\u7269\u4ef6\u9019\u500b\u8a5e\uff0c\u8b93\u6211\u53c8\u958b\u59cb\u601d\u8003\u9019\u662f\u751a\u9ebc\u6771\u897f\uff0c\u4e4b\u524d\u6709\u6c92\u6709\u8b1b\u904e\u3002\u300d<br \/>\n\u770b\u5230\u5c0f\u5149\u9019\u9ebc\u8a8d\u771f\u7684\u601d\u8003\u9019\u4e8b\u60c5\u5927\u982d\u96d6\u7136\u958b\u5fc3\u9019\u5c0f\u5b50\u5f88\u8a8d\u771f\u5728\u5b78\u7fd2\uff0c\u4f46\u662f\u4e5f\u89ba\u5f97\u4ed6\u7684\u6a23\u5b50\u5f88\u6709\u8da3\u6240\u4ee5\u5fcd\u4e0d\u4f4f\u5927\u7b11\u8d77\u4f86\uff0c\u6240\u4ee5\u5927\u982d\u5c31\u908a\u7b11\u8457\u8ddf\u5c0f\u5149\u9019\u9ebc\u8aaa\u3002<br \/>\n\u300c\u4f60\u9019\u5c0f\u5b50\u4e0d\u932f\uff0c\u4e0d\u904e\u95dc\u65bc\u751a\u9ebc\u662f\u7269\u4ef6\uff0c\u9019\u500b\u7b49\u660e\u5929\u518d\u544a\u8a34\u4f60\u4e86\u3002\u300d<br \/>\n\u6240\u4ee5\u8b80\u8005\u5011\u60f3\u8981\u4e86\u89e3\u751a\u9ebc\u662f\u7269\u4ef6\u7684\u8a71\uff0c\u656c\u8acb\u671f\u5f85\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8cc7\u6599\u7d50\u69cb\u4e4b\u5f8c\u5462 \u5728\u6628\u5929\u5927\u982d\u8ddf\u5c0f\u5149\u4ecb\u7d39\u4e86\u8cc7\u6599\u7d50\u69cb\u7684\u5167\u5bb9\uff0c\u4f46\u662f\u6709\u4e9b\u6771\u897f\u5927\u982d\u6c92\u6709\u8ddf\u5c0f\u5149\u7279\u5225\u89e3\u91cb\uff0c\u4f8b\u5982\u4ecb\u9762\u7269\u4ef6\u4e4b\u985e\u7684\uff0c &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2021\/09\/22\/method-property\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008D-23 \u65b9\u6cd5 method ? property\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[90,71,92],"class_list":["post-2087","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2021ironman","tag-c","tag-dotnetcore"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=2087"}],"version-history":[{"count":24,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2087\/revisions"}],"predecessor-version":[{"id":2943,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2087\/revisions\/2943"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=2087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=2087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=2087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}