{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/enum-csharp/","result":{"data":{"markdownRemark":{"id":"0099d97a-fedc-5e4a-bad6-5a1c122dae72","excerpt":"Enum is a typed constant that is specified for the new data form of Enumeration. An effective way to describe a set of named integral constants assigned to a…","html":"<p>Enum is a typed constant that is specified for the new data form of Enumeration. An effective way to describe a set of named integral constants assigned to a variable is to include a Typesafe enumeration. Enums make the code more readable and less vulnerable to mistakes. If you have a set of functionally important and unchanged values, Enums are useful for developers.</p>\n<p>Enums' key benefit is to make it possible in the future to adjust values. Enums are a robust alternative to the short String or Int constants used to describe sets of similar objects in far older APIs.</p>\n<p>Sometimes we must use the constant variable in our application to not be changed throughout the application. One of the methods to declare the continuous variables are <code>Enum.</code> Let's discuss it.</p>\n<h2 id=\"using-enum-in-c-sharp\" style=\"position:relative;\"><a href=\"#using-enum-in-c-sharp\" aria-label=\"using enum in c sharp permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Using Enum in C Sharp</h2>\n<p>Enum is the short form of Enumeration. Enum consumes less memory and space because they are value types, which means they store memory value. Also, Enums are strongly typed named constants. </p>\n<p>Enums written by developers are used in the <a href=\"https://en.wikipedia.org/wiki/.NET_Framework\">.NET system</a> to develop numeric constants. A numeric value must be allocated to all the members of an enum. Here are the main points about Enums:</p>\n<ul>\n<li>In C#, Keyword Enums build enumerated types of data</li>\n<li>Enums are made for developers</li>\n<li>Enums are strongly typed constants, as described above.</li>\n<li>An enum of one form cannot be allocated automatically to another type of enum.</li>\n<li>Enum values are fixed</li>\n</ul>\n<p><strong>Enums are of two types in C#</strong></p>\n<p><strong>Simple Enum</strong> - The members of this enum contain a single value.</p>\n<p><strong>Flags Enum</strong> - The members of this enum contain multiple values or multiple values combined using a bitwise OR operator. These enums are often used for bitwise operators.</p>\n<p>Here we will talk about the Simple Enum only.</p>\n<h2 id=\"how-to-declare-an-enum\" style=\"position:relative;\"><a href=\"#how-to-declare-an-enum\" aria-label=\"how to declare an enum permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to declare an Enum</h2>\n<p>Enum is declared by the <strong>enum</strong> keyword followed by the enum name and then its enum members. Like an example below</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">enum</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Vehicle</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">{</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">Car</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">Bike</span><span class=\"mtk1\">=</span><span class=\"mtk7\">3</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">Truck</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">Taxi</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>In the above code, an enum for the Vehicle is created. The enum values are started from <code>0</code> by default if we do not assign them the values and incremented by 1 for the next enum member. </p>\n<p>That means the <code>Car</code> enum has the value <code>0</code> and the <code>Bike</code> enum assigned the value <code>3</code> that means the <code>Bike</code> enum now has the value <code>3</code> and the next enums are incremented by 1 means <code>Truck</code> and <code>Taxi</code> enum have the value <code>4</code> and <code>5</code> respectively.</p>\n<p>By default, the type for enum elements is <strong>int</strong>. We can set different type by adding a colon like an example below. </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">enum</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Vehicle</span><span class=\"mtk1\">: </span><span class=\"mtk4\">byte</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">{</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">Car</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">Bike</span><span class=\"mtk1\">=</span><span class=\"mtk7\">3</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">Truck</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">Taxi</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>The different types which can be set are sbyte, byte, short, ushort, uint, ulong, and long.</p>\n<h2 id=\"get-the-value-of-an-enum\" style=\"position:relative;\"><a href=\"#get-the-value-of-an-enum\" aria-label=\"get the value of an enum permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Get the value of an Enum</h2>\n<p>To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the <code>ToString()</code> method as below.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">((</span><span class=\"mtk4\">int</span><span class=\"mtk1\">)</span><span class=\"mtk12\">VehicleEnum</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Car</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">string</span><span class=\"mtk1\"> </span><span class=\"mtk12\">vehicle</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">Vehicle</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Bike</span><span class=\"mtk1\">.</span><span class=\"mtk11\">ToString</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">(</span><span class=\"mtk12\">vehicle</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>Output: </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">0</span>\n<span class=\"grvsc-line\">Bike</span></code></pre>\n<h2 id=\"parse-the-string-value-into-an-enum\" style=\"position:relative;\"><a href=\"#parse-the-string-value-into-an-enum\" aria-label=\"parse the string value into an enum permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Parse the string value into an Enum</h2>\n<p>For parsing the string value into enum we have 2 methods</p>\n<ol>\n<li><em>Enum.Parse(Type enumType,string value)</em> - This method directly parses the string representation of the name or numeric value of enum member into enumType object. If the string representation of the name is not found, then it will give the exception.</li>\n</ol>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">(</span><span class=\"mtk12\">Enum</span><span class=\"mtk1\">.</span><span class=\"mtk11\">Parse</span><span class=\"mtk1\">(</span><span class=\"mtk4\">typeof</span><span class=\"mtk1\">(</span><span class=\"mtk10\">Vehicle</span><span class=\"mtk1\">),</span><span class=\"mtk8\">&quot;Car&quot;</span><span class=\"mtk1\">));</span></span></code></pre>\n<p>Output:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">Car</span></code></pre>\n<ol start=\"2\">\n<li><em>Enum.TryParse(string? value,out enumType result)</em> - This method gives the result in bool value. if the string representation of the name or numeric value of enum member into enumType object is parsed, then the result will be true, and the parsed value will be in the <code>out</code> variable. If the string value is not parsed, then the result will be false.</li>\n</ol>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">Enum</span><span class=\"mtk1\">.</span><span class=\"mtk11\">TryParse</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;0&quot;</span><span class=\"mtk1\">,</span><span class=\"mtk4\">out</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Vehicle</span><span class=\"mtk1\"> </span><span class=\"mtk12\">value</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">(</span><span class=\"mtk12\">value</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>Output:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">Car</span></code></pre>\n<p><strong>Note</strong> - Both the methods also have overload methods.</p>\n<h2 id=\"check-if-a-string-value-is-defined-in-the-enum\" style=\"position:relative;\"><a href=\"#check-if-a-string-value-is-defined-in-the-enum\" aria-label=\"check if a string value is defined in the enum permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Check if a string value is defined in the Enum</h2>\n<p>We can check if a given integral value, or its name as a string, exists in a specified enum.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">(</span><span class=\"mtk12\">Enum</span><span class=\"mtk1\">.</span><span class=\"mtk11\">IsDefined</span><span class=\"mtk1\">(</span><span class=\"mtk4\">typeof</span><span class=\"mtk1\">(</span><span class=\"mtk10\">Vehicle</span><span class=\"mtk1\">), </span><span class=\"mtk7\">0</span><span class=\"mtk1\">));</span></span></code></pre>\n<p>OR</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">(</span><span class=\"mtk12\">Enum</span><span class=\"mtk1\">.</span><span class=\"mtk11\">IsDefined</span><span class=\"mtk1\">(</span><span class=\"mtk4\">typeof</span><span class=\"mtk1\">(</span><span class=\"mtk10\">Vehicle</span><span class=\"mtk1\">), </span><span class=\"mtk8\">&quot;car&quot;</span><span class=\"mtk1\">));</span></span></code></pre>\n<p>Output:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">True</span></code></pre>\n<h2 id=\"loop-through-all-the-enum\" style=\"position:relative;\"><a href=\"#loop-through-all-the-enum\" aria-label=\"loop through all the enum permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Loop through all the Enum</h2>\n<p>For looping through the enums you can write the below code -</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"11\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">foreach</span><span class=\"mtk1\"> (</span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">data</span><span class=\"mtk1\"> </span><span class=\"mtk15\">in</span><span class=\"mtk1\"> </span><span class=\"mtk12\">Enum</span><span class=\"mtk1\">.</span><span class=\"mtk11\">GetNames</span><span class=\"mtk1\">(</span><span class=\"mtk4\">typeof</span><span class=\"mtk1\">(</span><span class=\"mtk10\">Vehicle</span><span class=\"mtk1\">)))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">{</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">(</span><span class=\"mtk12\">data</span><span class=\"mtk1\">+</span><span class=\"mtk8\">&quot; - &quot;</span><span class=\"mtk1\">+ (</span><span class=\"mtk4\">int</span><span class=\"mtk1\">)</span><span class=\"mtk12\">Enum</span><span class=\"mtk1\">.</span><span class=\"mtk11\">Parse</span><span class=\"mtk1\">(</span><span class=\"mtk4\">typeof</span><span class=\"mtk1\">(</span><span class=\"mtk10\">Vehicle</span><span class=\"mtk1\">), </span><span class=\"mtk12\">data</span><span class=\"mtk1\">));</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>Here <em>Enum.GetNames(Type enumType)</em> method is used, which retrieves an array of the names from the specified enum.</p>\n<p>Output:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"12\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">Car - 0</span>\n<span class=\"grvsc-line\">Bike - 3</span>\n<span class=\"grvsc-line\">Truck - 4</span>\n<span class=\"grvsc-line\">Taxi - 5</span></code></pre>\n<h2 id=\"call-an-enum-by-the-integral-value\" style=\"position:relative;\"><a href=\"#call-an-enum-by-the-integral-value\" aria-label=\"call an enum by the integral value permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Call an enum by the integral value</h2>\n<p>We can call the enum member by its integral value. If there is no corresponding value related to that integral value, then it will print that integral value.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"13\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\"> </span><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">((</span><span class=\"mtk10\">Vehicle</span><span class=\"mtk1\">)</span><span class=\"mtk7\">3</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>Output</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"14\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">Bike</span></code></pre>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>The advantages of using enums are that they are very easy to use and represented as strings but processed as integers. Enums are easy to maintain and improve code readability because they provide symbolic named constants, which means you need to remember the names, not the integer values.</p>\n<p>If you want to learn more about C programming, here is another article written on C# <a href=\"https://www.loginradius.com/blog/engineering/exception-handling-in-csharp/\">Exceptions and Exception Handling in C#</a> I hope you learn something new today and will going to try it out, if you have any questions feel free to drop a comment below.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n</style>","headings":[{"value":"Using Enum in C Sharp","depth":2},{"value":"How to declare an Enum","depth":2},{"value":"Get the value of an Enum","depth":2},{"value":"Parse the string value into an Enum","depth":2},{"value":"Check if a string value is defined in the Enum","depth":2},{"value":"Loop through all the Enum","depth":2},{"value":"Call an enum by the integral value","depth":2},{"value":"Conclusion","depth":2}],"fields":{"slug":"/engineering/enum-csharp/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":"Would you like to become more proficient in your C# programming in the use of enums? To learn the basics and use cases for Enum in C#, read this post.","title":"How to Use Enum in C#","canonical":null,"date":"December 18, 2020","updated_date":null,"tags":["C#","Enum"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/4730f4a49850d621c2969f5405e589e1/2ad7f/coverimage.webp","srcSet":"/static/4730f4a49850d621c2969f5405e589e1/1c9b5/coverimage.webp 200w,\n/static/4730f4a49850d621c2969f5405e589e1/f1752/coverimage.webp 400w,\n/static/4730f4a49850d621c2969f5405e589e1/2ad7f/coverimage.webp 800w,\n/static/4730f4a49850d621c2969f5405e589e1/b13b4/coverimage.webp 900w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Hemant Manwani","github":"hemant404","bio":"Hemant Manwani is a Software Engineer at LoginRadius who is interested in how the web works and developer tools that make working with web easier.","avatar":null}}}},"pageContext":{"id":"0099d97a-fedc-5e4a-bad6-5a1c122dae72","fields__slug":"/engineering/enum-csharp/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}