{"id":210056,"date":"2021-01-25T10:57:33","date_gmt":"2021-01-25T02:57:33","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=210056"},"modified":"2021-01-24T14:58:30","modified_gmt":"2021-01-24T06:58:30","slug":"bit-enumeration-overview","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/bit-enumeration-overview.html","title":{"rendered":"C# \u679a\u4e3e\u4e0e\u4f4d\u679a\u4e3e\u6982\u8ff0"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nC# \u679a\u4e3e\uff08Enum\uff09, \u679a\u4e3e\u7c7b\u578b\u662f\u7528\u4e8e\u58f0\u660e\u4e00\u7ec4\u547d\u540d\u7684\u5e38\u6570\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u503c\u7c7b\u578b\uff09\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4e00\u3001\u679a\u4e3e\u7684\u6982\u5ff5<\/strong><\/div>\n

C# \u679a\u4e3e\uff08Enum\uff09, \u679a\u4e3e\u7c7b\u578b\u662f\u7528\u4e8e\u58f0\u660e\u4e00\u7ec4\u547d\u540d\u7684\u5e38\u6570\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u503c\u7c7b\u578b\uff09\uff1b<\/p>\n

\u4e8c\u3001\u679a\u4e3e\u7684\u5b9a\u4e49<\/strong><\/div>\n

\u58f0\u660e enum \u53d8\u91cf\uff1a\u3000<\/p>\n

enum  {enumeration list};<\/enum_name><\/pre>\n

\u5176\u4e2d enum_name \u6307\u5b9a\u679a\u4e3e\u7684\u7c7b\u578b\u540d\u79f0\uff0cenumeration list \u662f\u4e00\u4e2a\u7528\u9017\u53f7\u5206\u9694\u7684\u6807\u8bc6\u7b26\u5217\u8868\uff1b
\n\u679a\u4e3e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u7b26\u53f7\u4ee3\u8868\u4e00\u4e2a\u6574\u6570\u503c\uff0c\u4e00\u4e2a\u6bd4\u4ed6\u524d\u9762\u7684\u7b26\u53f7\u5927\u7684\u6574\u6570\u503c\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7b2c\u4e00\u4e2a\u679a\u4e3e\u7b26\u53f7\u7684\u503c\u662f 0\u3002\u4f8b\u5982\uff1a<\/p>\n

enum Days {Sun , Mon, tue, Fir, sat };<\/pre>\n
\u4e09\u3001\u679a\u4e3e\u7684\u6848\u4f8b\uff0c\u679a\u4e3e\u53d8\u91cf\u7684\u7528\u6cd5<\/strong><\/div>\n

\u5b9e\u4f8b<\/p>\n

using System;\r\nnamespace EnumApplication\r\n{\r\n    class EnumProgram\r\n    {\r\n          enum Days { Sun, Mon, tue, Wed, thu, Fri, sat };\r\n         \r\n          static void Main(string[] args)\r\n          {\r\n                int WeekdayStart = (int)Days.Mon;\r\n                int WeekdayEnd = (int)Days.Fir;\r\n                Console.WriteLine(\"Monday\uff1a{0}\",WeekdayStart);  \r\n                Console.WriteLine(\"Firday\uff1a{0}\",WeekdayEnd );  \r\n                Console.ReadKey();\r\n          }    \r\n    }\r\n}<\/pre>\n
\u56db\u3001Enum \u7c7b\uff1a<\/strong><\/div>\n

\u6240\u6709\u679a\u4e3e\u7c7b\u578b\u90fd\u9690\u5f0f\u5730\u7ee7\u627f System.Enum \u7c7b\u578b\uff0c System.Enum \u7c7b\u578b\u662f\u7ee7\u627f\u81ea System.ValueType \u7c7b\u578b\u552f\u4e00\u4e0d\u4e3a\u503c\u7c7b\u578b\u7684\u5f15\u7528\u7c7b\u578b\uff1b
\n\"\"<\/p>\n

\u4e94\u3001Enum \u7c7b<\/strong><\/div>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\u65b9\u6cd5<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n
\nCompareTo<\/td>\n\u5c06\u6b64\u5b9e\u4f8b\u4e0e\u6307\u5b9a\u5bf9\u8c61\u8fdb\u884c\u6bd4\u8f83\u5e76\u8fd4\u56de\u4e00\u4e2a\u5bf9\u4e8c\u8005\u7684\u76f8\u5bf9\u503c\u7684\u6307\u793a<\/td>\n<\/tr>\n
\nEquals\uff1a\u6307\u793a\u6b64\u5b9e\u4f8b\u662f\u5426\u7b49\u4e8e\u6307\u5b9a\u7684\u5bf9\u8c61\uff1b\n<\/td>\n<\/tr>\n
Format<\/td>\n\u6839\u636e\u6307\u5b9a\u683c\u5f0f\u5c06\u6307\u5b9a\u679a\u4e3e\u7c7b\u578b\u7684\u6307\u5b9a\u503c\u8f6c\u6362\u4e3a\u5176\u7b49\u6548\u7684\u5b57\u7b26\u4e32\u8868\u793a\u5f62\u5f0f<\/p>\n<\/td>\n<\/tr>\n
GetName<\/td>\n\u5728\u6307\u5b9a\u679a\u4e3e\u4e2d\u68c0\u7d22\u5177\u6709\u6307\u5b9a\u503c\u7684\u5e38\u6570\u7684\u540d\u79f0<\/p>\n<\/td>\n<\/tr>\n
GetNames<\/td>\n\u68c0\u7d22\u6307\u5b9a\u679a\u4e3e\u4e2d\u5e38\u6570\u540d\u79f0\u7684\u6570\u7ec4<\/p>\n<\/td>\n<\/tr>\n
GetTypeCode<\/td>\n\u8fd4\u56de\u6b64\u5b9e\u4f8b\u7684\u57fa\u7840TypeCode<\/p>\n<\/td>\n<\/tr>\n
GetUnderlyingTye<\/td>\n\u8fd4\u56de\u6307\u5b9a\u679a\u4e3e\u7684\u57fa\u7840\u7c7b\u578b<\/p>\n<\/td>\n<\/tr>\n
GetValues<\/td>\n\u6307\u5b9a\u679a\u4e3e\u4e2d\u5e38\u6570\u503c\u7684\u6570\u7ec4<\/p>\n<\/td>\n<\/tr>\n
HasFlag<\/td>\n\u786e\u5b9a\u5f53\u524d\u5b9e\u4f8b\u4e2d\u662f\u5426\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u6216\u591a\u4e2a\u4f4d\u57df<\/p>\n<\/td>\n<\/tr>\n
IsDefined<\/td>\n\u8fd4\u56de\u6307\u5b9a\u679a\u4e3e\u4e2d\u662f\u5426\u5b58\u5728\u5177\u6709\u6307\u5b9a\u503c\u7684\u5e38\u6570\u7684\u6307\u793a<\/p>\n<\/td>\n<\/tr>\n
Parse<\/td>\n\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u679a\u4e3e\u5e38\u6570\u7684\u540d\u79f0\u6216\u6570\u5b57\u503c\u5f97\u5b57\u7b26\u4e32\u8868\u793a\u8f6c\u6362\u6210\u7b49\u6548\u5f97\u679a\u4e3e\u5bf9\u8c61\uff0c\u4e00\u4e2a\u53c2\u6570\u6307\u5b9a\u8be5\u64cd\u4f5c\u662f\u5426\u4e0d\u533a\u5206\u5927\u5c0f\u5199<\/p>\n<\/td>\n<\/tr>\n
TryParse<\/td>\n\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u679a\u4e3e\u5e38\u6570\u5f97\u540d\u79f0\u6216\u6570\u5b57\u503c\u5f97\u5b57\u7b26\u4e32\u8868\u793a\u8f6c\u6362\u6210\u7b49\u6548\u5f97\u679a\u4e3e\u5bf9\u8c61\uff0c\u7528\u4e8e\u6307\u793a\u8f6c\u6362\u662f\u5426\u6210\u529f\u5f97\u8fd4\u56de\u503c\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u516d\u3001\u6807\u5fd7\u679a\u4e3e\/\u4f4d\u679a\u4e3e<\/strong><\/div>\n

C# \u6807\u8bb0\u679a\u4e3e\uff08Flags)\uff1a\u679a\u4e3e\u7c7b\u578b\u662f\u7528\u4e8e\u58f0\u660e\u4e00\u7ec4\u547d\u540d\u5f97\u5e38\u6570\u5f97\u57fa\u672c\u7c7b\u578b\u6570\u636e\uff08\u503c\u7c7b\u578b\uff09\u3002<\/p>\n

\u679a\u4e3e\u503c\u662f\u4e92\u65a5\u5f97\u3002\u800c\u4f4d\u6807\u8bb0\u96c6\u5408\u662f\u4e00\u79cd\u7531\u7ec4\u5408\u51fa\u73b0\u5f97\u5143\u7d20\u5f62\u6210\u5f97\u5217\u8868\uff0c\u901a\u5e38\u8bbe\u8ba1\u4e3a\u4ee5\"\u4f4d\u6216\"\u8fd0\u7b97\u7ec4\u5408\u65b0\u503c\u3002<\/p>\n

\u3000\u3000
\n\u679a\u4e3e\u7c7b\u578b\u5219\u901a\u5e38\u8868\u8fbe\u4e00\u79cd\u8bed\u4e49\u76f8\u5bf9\u72ec\u7acb\u5f97\u6570\u503c\u96c6\u5408\u3002\u800c\u4ee5\u679a\u4e3e\u7c7b\u578b\u6765\u5b9e\u73b0\u4f4d\u6807\u8bb0\u96c6\u5408\u662f\u6700\u5b8c\u7f8e\u5f97\u7ec4\u5408\uff0c\u7b80\u79f0\u4f4d\u679a\u4e3e\u3002<\/p>\n

\u4e03\u3001\u4f4d\u679a\u4e3e\u5f97\u5b9a\u4e49\u3000<\/strong><\/div>\n

\u3000<\/p>\n

\/\/\/\r\n\u3000\u3000\/\/\/ \u6743\u9650\u679a\u4e3e\r\n\u3000\u3000\/\/\/<\/summary>\r\n\u3000\u3000[Flags]\r\n\u3000\u3000public enum permission\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000 \u6ce8\u610f\u52a0\u4e86[Flags]\u7279\u6027\u540e\u6709\u4e09\u79cd\u5199\u6cd5\uff0c\r\n\u3000\u3000{\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000 \u4e00\u79cd\u662f\u4f7f\u7528<<\u7b26\u53f7\uff0c\r\n  \u3000\u3000  Unknown = 0; \/\/ \u4e5f\u53ef\u4ee5\u5199\u62100x00\u62160\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u7b2c\u4e8c\u79cd\u662f0x01,\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\r\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u8fd8\u6709\u4e00\u79cd\u662f\u76f4\u63a5\u51990\uff0c1\uff0c2\uff0c4\uff0c8....\uff0c\r\n   \u3000\u3000 Create = 1<<0, \/\/ 0x01\u62161\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u4e00\u822c\u6765\u8bf4\u662f2\u7684n\u6b21\u65b9\u6765\u8868\u793a\u3002\u3000\u3000\r\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000  \u6ce8\uff1a<<\u5de6\u64cd\u4f5c\u7b26\uff0c\u8868\u793a\u5bf9\u8fd9\u4e2a\u6570\u8fdb\u884c\u79fb\u4f4d\u3002\r\n    \u3000\u3000Read = 1<<1,  \/\/0x02\u62162\r\n\r\n   \u3000\u3000 Update = 1<<2, \/\/0x04\u62164\r\n\r\n   \u3000\u3000 Delete = 1<<3  \/\/0x08\u62168\r\n\u3000\u3000}<\/pre>\n
\u516b\u3001\u4f4d\u679a\u4e3e\u7684\u5b9e\u4f8b<\/strong><\/div>\n
\/\/1\u3001\u7ed9\u7528\u6237\u521b\u5efa\u3001\u8bfb\u53d6\uff0c\u4fee\u6539\u548c\u5220\u9664\u7684\u6743\u9650\r\nvar parmission = Permission.Create | parmission.Read | parmission.Update | parmission.Delete;\r\n\r\n\/\/2\u3001\u53bb\u6389\u7528\u6237\u7684\u4fee\u6539\u548c\u5220\u9664\u6743\u9650\r\nparmission = parmission &~parmission.Update;\r\nparmission = parmission &~parmission.Delete;\r\n\r\n\/\/3\u3001\u7ed9\u7528\u6237\u52a0\u4e0a\u4fee\u6539\u7684\u6743\u9650\r\nparmission = parmission | parmission.Update;\r\n\r\n\/\/4\u3001\u5224\u65ad\u7528\u6237\u662f\u5426\u6709\u521b\u5efa\u7684\u6743\u9650\r\nvar isCreate = (parmission & parmission.Create)!=0;\r\n\/\/\u6216\u8005\r\nvar isCreate = (parmission & parmission.Create)==parmission.Create;\r\n\u3000\u3000\u8fd9\u65f6parmission\u679a\u4e3e\u7684\u503c\u5c06\u53d8\u62100+1+4=5\uff0c\u5b83\u7684ToSting()\u5c06\u53d8\u6210\u201cparmission.Create\uff0c\r\n\u3000\u3000parmission.Read\u201d,parmission.Update; \u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u89e3\u91ca\u4e3a\u4ec0\u4e48\u7b2c\u4e94\u4e2a\u503cDelete\u662f8\u800c\u4e0d\u80fd\u6210\u4e3a5\u3002\r\n\u3000\u3000\u4e5f\u5c31\u662f\u8bf4\u5b83\u7684\u503c\u4e0d\u5e94\u8be5\u662f\u524d\u51e0\u9879\u503c\u7684\u590d\u5408\u503c\u3002\u4e00\u4e2a\u6bd4\u8f83\u7b80\u5355\u7684\u65b9\u6cd5\r\n\u3000\u3000\u5c31\u662f\u75282\u7684n\u6b21\u65b9\u6765\u4f9d\u6b21\u4f4d\u6bcf\u4e00\u9879\u8d4b\u503c\uff0c\u4f8b\u5982\uff1a1\uff0c2\uff0c4\uff0c8\uff0c16\uff0c32\uff0c64.......\u3002<\/pre>\n","protected":false},"excerpt":{"rendered":"

C# \u679a\u4e3e\uff08Enum\uff09, \u679a\u4e3e\u7c7b\u578b\u662f\u7528\u4e8e\u58f0\u660e\u4e00\u7ec4\u547d\u540d\u7684\u5e38\u6570\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u503c\u7c7b\u578b\uff09\uff1b \u58f0\u660e enum \u53d8\u91cf\uff1a\u3000 […]<\/p>\n","protected":false},"author":321,"featured_media":210058,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-210056","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=210056"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210056\/revisions"}],"predecessor-version":[{"id":210061,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210056\/revisions\/210061"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/210058"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=210056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=210056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=210056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}