Coding Villa

Compare enumerations in c#

Author : Hassan Shafiq

Share with Friends
Views : 550
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this simple article you will learn that how we can Compare enumerations in c#.


Advertisements


 

 

Enumeration:

 

Enumeration provides the best way to define a set of named integral constants which we assigned to a variable by using the enum keyword before variable. For an instance, if we define a variable whose value will represent the name of the countries then we can declare enumeration in this way

 

enum CountryNames { Pakistan, Japan, Italy, Spain};

 

ENUM keyword

 

We use the enum keyword to declare an enumeration. The default type of each  element in the enum is int. However, we can also assign other integral numeric type by using a colon. For example,

 

enum Volume :byte{ Low =20,Medium =24, High  =33}

 

 

The following code will show the comparison of enumeration.

 

CODE:

 

using System;

 

class Program

    {

 

 enum Age

        {

 

            Teenager = 14,

            Adult = 18,

            Old = 50

 

        }

        static void Main(string[] args)

        {

            Age Ali = Age.Teenager;

            Age Hassan = Age.Adult;

 

            if (Ali < Hassan)

                Console.WriteLine("Ali is younger than Hassan");

            else

                Console.WriteLine("Ali is not younger than Hassan");

            Console.ReadLine();

        }

    }

 

 



Article Tags
Share with Friends

Comments


No Image Hafiz Nauman
Should not we use Struct or Class structures using C/C++ ???

Dont you think Enum type is so limited as compared to these user define Data-Types?
if yes,so kindly let me know Why do we use Enum type and what is its advantage over Struct or Class, if any?


Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook