blog

Home / DeveloperSection / Blogs / Enumerations in c#

Enumerations in c#

priyanka kushwaha 2153 03-Mar-2015

This blog explains how enumerations works in c#.

Definition:

Enum is a keyword that represent a value type for declaring a set of named constant.

An enum helps to define a series of related integral constants that represent special values within a module of code. An enum can be used in a switch statement, which is used as a decision-making statement for comparing numeric values.

Example:
1. Create a Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EnumExample.Default" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <formid="form1"runat="server">

 

        <table>

            <tr>

                <tdcolspan="2">Enter First Number</td>

                <tdcolspan="2">

                    <asp:TextBoxID="txtboxfirst"runat="server"></asp:TextBox></td><td><asp:RequiredFieldValidatorID="RequiredFieldValidator1"runat="server"ErrorMessage="*"ControlToValidate="txtboxfirst"></asp:RequiredFieldValidator>

                    <asp:RegularExpressionValidatorID="RegularExpressionValidator1"runat="server"ErrorMessage="*"ValidationExpression="^[1-9]\d*(\.\d+)?$"ControlToValidate="txtboxfirst"></asp:RegularExpressionValidator></td>

            </tr>

            <tr>

                <tdcolspan="2">Enter Second number</td>

                <tdcolspan="2">

                    <asp:TextBoxID="txtboxsencond"runat="server"></asp:TextBox></td><td>

                    <asp:RequiredFieldValidatorID="RequiredFieldValidator2"runat="server"ErrorMessage="*"ControlToValidate="txtboxsencond"></asp:RequiredFieldValidator>

                    <asp:RegularExpressionValidatorID="RegularExpressionValidator2"runat="server"ErrorMessage="*"ValidationExpression="^[1-9]\d*(\.\d+)?$"ControlToValidate="txtboxsencond"></asp:RegularExpressionValidator>

                </td>

            </tr>

            <tr>

                <tdcolspan="2">Result</td>

                <tdcolspan="2">

                    <asp:TextBoxID="txtResult"runat="server"Enabled="false"></asp:TextBox></td>

            </tr>

            <tr>

                <td>

                    <asp:ButtonID="btnAdd"runat="server"Text="+"OnClick="btnAdd_Click"Width="49px"/></td>

                <td>

                    <asp:ButtonID="btnSub"runat="server"Text="-"OnClick="btnAdd_Click"Width="49px"/></td>

                <td>

                    <asp:ButtonID="btnMul"runat="server"Text="*"OnClick="btnAdd_Click"Width="49px"/></td>

                <td>

                    <asp:ButtonID="btnDiv"runat="server"Text="/"OnClick="btnAdd_Click"Width="49px"/></td>

            </tr>

        </table>

    </form>

</body>

</html>

 

 

2.      Write in Default.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Collections;

namespace EnumExample

{

    public partial class Default : System.Web.UI.Page

    {

     

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        enum Operation { Add = '+', Mul = '*', Sub = '-', Div = '/' }


        protectedvoid btnAdd_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

 

//Convert button text to enum

            Operation type = (Operation)char.Parse(btn.Text);

            switch(type)

            {

caseOperation.Add:

                    txtResult.Text=Convert.ToString(Convert.ToDouble(txtboxfirst.Text) + Convert.ToDouble(txtboxsencond.Text));

                    break;

             caseOperation.Div:

                    txtResult.Text = Convert.ToString(Convert.ToDouble(txtboxfirst.Text) / Convert.ToDouble(txtboxsencond.Text));

                    break;

             caseOperation.Mul:

                    txtResult.Text = Convert.ToString(Convert.ToDouble(txtboxfirst.Text) * Convert.ToDouble(txtboxsencond.Text));

                    break;

             caseOperation.Sub:

                    txtResult.Text = Convert.ToString(Convert.ToDouble(txtboxfirst.Text) - Convert.ToDouble(txtboxsencond.Text));

                    break;

 

        }

        }

    }

}

 

Output:


Enumerations in c#

 

Advantage:

1.       In future, you can easily change values.

2.      Your code can be read easier.


Updated 22-Feb-2018

Leave Comment

Comments

Liked By