In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what is the use of C# bit domain", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn the article "what is the use of C# bit domain"?
The C # bit field is mainly used when there are multiple mixed states for a thing in .net, and a single enumeration is used when a thing has only a single attribute. In order to better implement the mixed state, we can add the C # bit field label to the enumeration. The following is the example we used in this article:
[Flags] public enum Week {[Description ("Monday")] Monday = 1
< < 0, [Description("星期二")] Tuesday = 1 < < 1, [Description("星期三")] Wednesday = 1 < < 2, [Description("星期四")] Tursday = 1 < < 3, [Description("星期五")] Friday = 1 < < 4, [Description("星期六")] Saturday = 1 < < 5, [Description("星期日")] Sunday = 1 < < 6 } 位域支持的运算符 1. "|":表示两边求并集(元素相加,相同元素只出现一次) Week week = Week.Tuesday | Week.Monday | Week.Monday; MessageBox.Show(Convert.ToString(week)); 这段代码的结果就是 Monday,Tuesday 2. "&":表示两边是否其中一个是另外一个的子集,如果是返回子集,否则返回0(如果其中一个包含另外一个,返回被包含的,否则返回0) week = Week.Monday & week; MessageBox.Show(week.ToString());与week = week & Week.Monday; MessageBox.Show(week.ToString()); 上面这两段代码的结果是相同的,如果week的初始值为:Monday,Tuesday,返回的结果为:Monday 3."^":表示从两者的并集中去除两者的交集(把两个的元素合并到一起,如果两个中有公共元素,要将这个公共元素从合并的结果中去除) week = (Week.Monday | Week.Wednesday)^ (Week.Tuesday | Week.Monday); MessageBox.Show(week.ToString()); week = (Week.Monday | Week.Wednesday) ^ (Week.Tuesday | Week.Sunday); MessageBox.Show(week.ToString()); 上面两个返回的结果应该为:Tuesday,Wednesday 和 Monday,Tuesday,Wednesday,Sunday 4."~":表示取反,返回的结果我还不知道应该是什么,以后再查一下。用法主要和"&"一起使用,例如:去除其中的某个元素 week = Week.Tuesday | Week.Monday | Week.Wednesday; week = week &(~Week.Monday); MessageBox.Show(week.ToString()); 上面返回的结果为:Tuesday,Wednesday 正逆转化 上面的内容存在数据库时我们可能为了简单只存取数字即可,例如:1表示Monday,3表示Monday,Tuesday。我们可以根据数据库里面的值方便获取存储的内容,代码如下: week = Week.Monday | Week.Tuesday; MessageBox.Show(Convert.ToString((int)week)); week = (Week)Enum.Parse(typeof(Week), "10"); MessageBox.Show(week.ToString()); 返回的结果为:3 和 Tuesday,Tursday 获取Description标签内容 我们既然可以给里面的值加上Description,就可以在程序中获取到这个内容,至于用途,大家自己看吧,东西摆出来,大家自己随便怎么用,下面的代码是从网上找到的,内容如下: /// < summary>/ / read out and return a key-value pair from the enumerated type and its properties.
< /summary>/ / /
< param name="enumType">Type, which is in the format typeof (enumerated type to be read)
< /param>/ / /
< returns>Key-value pair
< /returns>Public static NameValueCollection GetNVCFromEnumValue (Type enumType) {NameValueCollection nvc = new NameValueCollection (); Type typeDescription = typeof (DescriptionAttribute); System.Reflection.FieldInfo [] fields = enumType.GetFields (); string strText = string.Empty; string strValue = string.Empty; foreach (FieldInfo field in fields) {if (field.FieldType.IsEnum) {strValue = (int) enumType.InvokeMember (field.Name, BindingFlags.GetField, null, null, null). ToString (); object [] arr = field.GetCustomAttributes (typeDescription, true) If (arr.Length > 0) {DescriptionAttribute aa = (DescriptionAttribute) arr [0]; strText = aa.Description;} else {strText = field.Name;} nvc.Add (strText, strValue);}} return nvc;} above is all the content of this article, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.