{"id":73579,"date":"2022-08-10T09:14:09","date_gmt":"2022-08-10T07:14:09","guid":{"rendered":"https:\/\/drafts.code-maze.com\/?p=73579"},"modified":"2022-08-10T09:14:09","modified_gmt":"2022-08-10T07:14:09","slug":"csharp-flags-attribute-for-enum","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/","title":{"rendered":"Flags Attribute For Enum in C#"},"content":{"rendered":"<p>In this article, we are going to learn about the Flags attribute for enum in C#, what its benefits are, and how to use it in a practical example.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/csharp-intermediate-topics\/FlagsAttributeForEnum\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let\u2019s start.<\/p>\n<h2><a id=\"definingtheproblem\"><\/a>Define the Problem<\/h2>\n<p id=\"tw-target-text\" class=\"tw-data-text tw-text-large tw-ta\" dir=\"ltr\" data-placeholder=\"Translation\">Let&#8217;s define an enum that represents the types of users:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public enum UserType\r\n{\r\n    Customer = 1,\r\n    Driver = 2,  \r\n    Admin = 3,\r\n}<\/pre>\n<p dir=\"ltr\" data-placeholder=\"Translation\">We define the <code>UserType<\/code> enum that contains three values: <code>Customer<\/code>, <code>Driver<\/code>, and <code>Admin<\/code>.<\/p>\n<p id=\"tw-target-text\" class=\"tw-data-text tw-text-large tw-ta\" dir=\"ltr\" data-placeholder=\"Translation\"><span class=\"Y2IQFc\" lang=\"en\">But what if we need to represent a collection of values?<\/span><\/p>\n<p dir=\"ltr\" data-placeholder=\"Translation\">For example, at a delivery company, we know that both the <code>Admin<\/code> and the <code>Driver<\/code> are employees. So let&#8217;s add a new enumeration item <code>Employee<\/code>. Later on, we will show you how we can represent both the admin and the driver with it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"6\">public enum UserType\r\n{   \r\n    Customer = 1,\r\n    Driver = 2,  \r\n    Admin = 3,\r\n    Employee = 4\r\n}<\/pre>\n<h2>Define and Declare a Flags Attribute<\/h2>\n<p id=\"tw-target-text\" class=\"tw-data-text tw-text-large tw-ta\" dir=\"ltr\" data-placeholder=\"Translation\"><span class=\"Y2IQFc\" lang=\"en\">\u00a0A <code>Flags<\/code> is <strong>an attribute that allows us to represent an enum as a collection of values \u200b\u200brather than a single value. <\/strong>So, let&#8217;s see how we can implement the <code>Flags<\/code> attribute on enumeration:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Flags]\r\npublic enum UserType\r\n{   \r\n    Customer = 1,\r\n    Driver = 2,  \r\n    Admin = 4,\r\n}<\/pre>\n<p>We add the <code>Flags<\/code> <span class=\"Y2IQFc\" lang=\"en\">attribute and number the values with powers of 2. Without both, this won&#8217;t work.<\/span><\/p>\n<p id=\"tw-target-text\" class=\"tw-data-text tw-text-large tw-ta\" dir=\"ltr\" data-placeholder=\"Translation\"><span class=\"Y2IQFc\" lang=\"en\">Now going back to our previous problem, we can represent <code>Employee<\/code> using the <code>|<\/code> operator:<\/span><\/p>\n<p dir=\"ltr\" data-placeholder=\"Translation\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var employee = UserType.Driver | UserType.Admin;<\/code><\/p>\n<p><span class=\"Y2IQFc\" lang=\"en\">Also, w<\/span>e can define it as a constant inside the enum to use it directly:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"7\">[Flags]\r\npublic enum UserType\r\n{                \r\n    Customer = 1,             \r\n    Driver = 2,               \r\n    Admin = 4,                \r\n    Employee = Driver | Admin\r\n}\r\n<\/pre>\n<h2><a id=\"behindthescenes\"><\/a>Behind the Scenes<\/h2>\n<p id=\"tw-target-text\" class=\"tw-data-text tw-text-large tw-ta\" dir=\"ltr\" data-placeholder=\"Translation\"><span class=\"Y2IQFc\" lang=\"en\">To understand the <code>Flags<\/code> attribute better, we must go back to the binary representation of the number. For example, we can represent <strong>1<\/strong> as binary <strong>0b_0001<\/strong> and <strong>2<\/strong> as <strong>0b_0010<\/strong>:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"4-7\">[Flags]\r\npublic enum UserType\r\n{\r\n    Customer = 0b_0001,\r\n    Driver = 0b_0010,\r\n    Admin = 0b_0100,\r\n    Employee = Driver | Admin, \/\/0b_0110\r\n}\r\n<\/pre>\n<p dir=\"ltr\" data-placeholder=\"Translation\">We can see that each value is represented in an active bit<span class=\"Y2IQFc\" lang=\"en\">. And this is where the idea of \u200b\u200bnumbering the values \u200b\u200bwith the power of 2 came from. We can also note that <code>Employee<\/code>contains two active bits, that is, it is a composite of two values <code>Driver<\/code> and <code>Admin<\/code>.<\/span><\/p>\n<h2><a id=\"oprerationsonflagsattribute\"><\/a>Operations on Flags Attribute<\/h2>\n<p id=\"tw-target-text\" class=\"tw-data-text tw-text-large tw-ta\" dir=\"ltr\" data-placeholder=\"Translation\">We can use the <strong>bitwise<\/strong> <strong>operators<\/strong> to work with <code>Flags<\/code>.<\/p>\n<h3 dir=\"ltr\" data-placeholder=\"Translation\"><a id=\"initializingavalue\"><\/a>Initialize a Value<\/h3>\n<p>For the initialization, we should use the value <strong>0 <\/strong>named <strong>None, <\/strong>which means the collection is empty:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"4\">[Flags]\r\npublic enum UserType\r\n{\r\n    None = 0,\r\n    Customer = 1,\r\n    Driver = 2,\r\n    Admin = 4,\r\n    Employee = Driver | Admin\r\n}\r\n<\/pre>\n<p>Now, we can define a variable:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var flags = UserType.None;<\/code><\/p>\n<h3><a id=\"addingavalue\"><\/a>Add a Value\u00a0<\/h3>\n<p>We can add value by using <code>|<\/code> operator:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">flags |= UserType.Driver;<\/code><\/p>\n<p>Now, the\u00a0 <code>flags<\/code> variable equals <code>Driver<\/code>.<\/p>\n<h3><strong><a id=\"removingavalue\"><\/a>Remove a Value<\/strong><\/h3>\n<p>We can remove value by use <code>&amp;<\/code>, <code>~<\/code> operators:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">flags &amp;= ~UserType.Driver;<\/code><\/p>\n<p>Now, <code>flags<\/code>variable equals <code>None<\/code>.<\/p>\n<h3><a id=\"checkingifthevalueexists\"><\/a>Check if the Value Exists<\/h3>\n<p>We can check if the value exists by using <code>&amp;<\/code> operator:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">Console.WriteLine((flags &amp; UserType.Driver) == UserType.Driver);<\/code><\/p>\n<p>The result is <strong>False<\/strong>.<\/p>\n<p>Also, we can do this by using the <code>HasFlag<\/code> method:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">Console.WriteLine(flags.HasFlag(UserType.Driver));<\/code><\/p>\n<p>Also, the result will be <strong>False<\/strong>.<\/p>\n<p>As we can see, both ways, using the <code>&amp;<\/code> operator and the <code>HasFlag<\/code> method, give the same result, but which one should we use? To find out, we will test the performance on several frameworks.<\/p>\n<h2>Measure the Performance<\/h2>\n<p>First, we will create a <strong>Console App, <\/strong>and in the <strong>.csproj <\/strong>file we will replace the <strong>TargetFramwork<\/strong> tag with the <strong>TargetFramworks <\/strong>tag:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">&lt;TargetFrameworks&gt;net48;netcoreapp3.1;net6.0&lt;\/TargetFrameworks&gt;<\/code><\/p>\n<p>We use the <strong>TargetFramworks <\/strong>tag to support multiple frameworks: .NET Framework 4.8, .Net Core 3.1, and .Net 6.0.<\/p>\n<p>Secondly, let\u2019s introduce the <a href=\"https:\/\/code-maze.com\/benchmarking-csharp-and-asp-net-core-projects\/\" target=\"_blank\" rel=\"noopener\">BenchmarkDotNet<\/a> library to get the benchmark results:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Benchmark]\r\npublic bool HasFlag()\r\n{\r\n    var result = false;\r\n    for (int i = 0; i &lt; 100000; i++)\r\n    {\r\n        result = UserType.Employee.HasFlag(UserType.Driver);\r\n    }\r\n\r\n    return result;\r\n}\r\n\r\n[Benchmark]\r\npublic bool BitOperator()\r\n{\r\n    var result = false;\r\n    for (int i = 0; i &lt; 100000; i++)\r\n    {\r\n        result = (UserType.Employee &amp; UserType.Driver) == UserType.Driver;\r\n    }\r\n\r\n    return result;\r\n}\r\n<\/pre>\n<p>We add <code>[SimpleJob(RuntimeMoniker.Net48)]<\/code>, <code>[SimpleJob(RuntimeMoniker.NetCoreApp31)]<\/code>, and <code>[SimpleJob(RuntimeMoniker.Net60)]<\/code> attributes to the <code>HasFlagBenchmarker<\/code> class to see the performance differences between different versions of .NET Framework \/ .NET Core:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">|      Method |                Job |            Runtime |        Mean |      Error |      StdDev |      Median |\r\n|------------ |------------------- |------------------- |------------:|-----------:|------------:|------------:|\r\n|     HasFlag |           .NET 6.0 |           .NET 6.0 |    37.79 us |   3.781 us |    11.15 us |    30.30 us |\r\n| BitOperator |           .NET 6.0 |           .NET 6.0 |    38.17 us |   3.853 us |    11.36 us |    30.38 us |\r\n|     HasFlag |      .NET Core 3.1 |      .NET Core 3.1 |    38.31 us |   3.939 us |    11.61 us |    30.37 us |\r\n| BitOperator |      .NET Core 3.1 |      .NET Core 3.1 |    38.07 us |   3.819 us |    11.26 us |    30.33 us |\r\n|     HasFlag | .NET Framework 4.8 | .NET Framework 4.8 | 2,893.10 us | 342.563 us | 1,010.06 us | 2,318.93 us |\r\n| BitOperator | .NET Framework 4.8 | .NET Framework 4.8 |    38.04 us |   3.920 us |    11.56 us |    30.17 us |\r\n<\/pre>\n<p>So, in <strong>.NET Framework 4.8<\/strong> a\u00a0<code>HasFlag<\/code> method was much slower than the <code>BitOperator<\/code>. But, the performance improves in <strong>.Net Core 3.1 <\/strong>and <strong>.Net 6.0.<\/strong> So in newer versions, we can use both ways.<\/p>\n<h2><a id=\"apracticalexampleofflagsattribute\"><\/a>A Practical Example of Flags Attribute<\/h2>\n<p>Let&#8217;s say we want to send messages to users by their type.<\/p>\n<p>We can define previous operations as <strong>Extension Methods<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public static UserType Add(this UserType userType, params UserType[] typesToAdd)\r\n{\r\n    foreach (var flag in typesToAdd)\r\n    {\r\n        userType |= flag;\r\n    }\r\n\r\n    return userType;\r\n}\r\n\r\npublic static UserType Remove(this UserType userType, params UserType[] typesToRemove)\r\n{\r\n    foreach (var item in typesToRemove)\r\n    {\r\n        userType &amp;= ~item;\r\n    }\r\n\r\n    return userType;\r\n}\r\n\r\npublic static bool CustomHasFlag(this UserType userType, UserType typeToCompare)\r\n    =&gt; (userType &amp; typeToCompare) == typeToCompare;\r\n\r\npublic static void Print(this UserType userType)\r\n    =&gt; Console.WriteLine(\"This message is for users of type: {0}.\", userType);\r\n<\/pre>\n<p>Now, we can use them easily:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var audience = UserType.None;\r\n\r\naudience = audince.Add(UserType.Employee, UserType.Customer);\r\naudience.Print();\r\n\r\naudience = audience.Remove(UserType.Driver);\r\naudience.Print();\r\n\r\nif (audience.CustomHasFlag(UserType.Driver)) \/\/ or use HasFlag\r\n{\r\n    Console.WriteLine(\"Driver exists.\");\r\n}\r\nelse\r\n{\r\n    Console.WriteLine(\"Driver doesn't exist.\");\r\n    Console.WriteLine(\"Adding Driver...\");\r\n    audience = audience.Add(UserType.Driver);\r\n}\r\naudience.Print();<\/pre>\n<p>And the output will be:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">This message is for users of type: Customer, Employee.\r\nThis message is for users of type: Customer, Admin.\r\nDriver doesn't exist.\r\nAdding Driver...\r\nThis message is for users of type: Customer, Employee.<\/pre>\n<h2><a id=\"conclusion\"><\/a>Conclusion<\/h2>\n<p>In this article, we\u2019ve learned about the <code>Flags<\/code> attribute in C# and operations we can do with it. We\u2019ve also compared two ways to check if a value exists. Finally, we\u2019ve used our knowledge in a single example to see the Flags attribute in action.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to learn about the Flags attribute for enum in C#, what its benefits are, and how to use it in a practical example. Let\u2019s start. Define the Problem Let&#8217;s define an enum that represents the types of users: public enum UserType { Customer = 1, Driver = 2, Admin [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12,506],"tags":[1391,1390,1389],"class_list":["post-73579","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","category-intermediate","tag-bitwise-operators","tag-enumeration","tag-flags-attribute","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Flags Attribute For Enum in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we are going to learn about flags attribute in C#. We will see how we can use it with enumeration and compare a performance\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Flags Attribute For Enum in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we are going to learn about flags attribute in C#. We will see how we can use it with enumeration and compare a performance\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-10T07:14:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"Flags Attribute For Enum in C#\",\"datePublished\":\"2022-08-10T07:14:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/\"},\"wordCount\":607,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"Bitwise operators\",\"Enumeration\",\"Flags attribute\"],\"articleSection\":[\"C#\",\"Intermediate\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/\",\"url\":\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/\",\"name\":\"Flags Attribute For Enum in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2022-08-10T07:14:09+00:00\",\"description\":\"In this article, we are going to learn about flags attribute in C#. We will see how we can use it with enumeration and compare a performance\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Flags Attribute For Enum in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Flags Attribute For Enum in C# - Code Maze","description":"In this article, we are going to learn about flags attribute in C#. We will see how we can use it with enumeration and compare a performance","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/","og_locale":"en_US","og_type":"article","og_title":"Flags Attribute For Enum in C# - Code Maze","og_description":"In this article, we are going to learn about flags attribute in C#. We will see how we can use it with enumeration and compare a performance","og_url":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/","og_site_name":"Code Maze","article_published_time":"2022-08-10T07:14:09+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"Flags Attribute For Enum in C#","datePublished":"2022-08-10T07:14:09+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/"},"wordCount":607,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["Bitwise operators","Enumeration","Flags attribute"],"articleSection":["C#","Intermediate"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/","url":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/","name":"Flags Attribute For Enum in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2022-08-10T07:14:09+00:00","description":"In this article, we are going to learn about flags attribute in C#. We will see how we can use it with enumeration and compare a performance","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-flags-attribute-for-enum\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Flags Attribute For Enum in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/73579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=73579"}],"version-history":[{"count":1,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/73579\/revisions"}],"predecessor-version":[{"id":73581,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/73579\/revisions\/73581"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=73579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=73579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=73579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}