Hello Friends, In this session we are going to discuss Enumeration in C#. We can see Enumeration as an alternate of constants in C#. Let’s discuss Enumeration with an example.

Introduction:

Enumeration is a user-defined Integer type that contains distinct value type. When we talked about Enumeration we talked about set of named Integer constants. The keyword enum is used to declare an enumerated type.

Enumeration

 

The syntax for declaring Enumeration in C#.

enum enumname {enumerationlist};

As you can see in the above syntax: enum keyword is used to declare an enumerated type. “enumname” specifies the name of the enum.” Enumerationlist” is the comma-separated list of named Integer types.

  • “enum” keyword is used to declare an enumerated type.
  • “enumname” specifies the name of the enum.” Enumerationlist” is the comma-separated list of named Integer types.
  • ” Enumerationlist” is the comma-separated list of named Integer types.

Since it is a set of named Integer constants, it provides an alternate way to Constants. Let’s understand how enumeration provides a way to us to replace constants.

Consider two constant variable in C#.

constant int Rate1=100;

constant int Rate2=200;

If you wish to add some more constants to the list such as:

constant int Rate3=300;

constant int Rate4=400;

constant int Rate5=500;

This is somewhere an awkward process and there is logical reason to declare various constants. This problem is overcome by Enumeration. We can achieve the same thing using enumeration.

enum RateEnum

{

Rate1=100,

Rate2=200,

Rate3=300,

Rate4=400,

Rate5=500

}

An example of Enumeration in C#:

Example 1:

Open Visual Studio and create a Console application project “EnumerationDemo”.

Write the following code in Program.cs file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EnumerationDemo
{
 class Program
 {
 enum Weekdays 
 {
 Monday=1,
 Tuesday=2,
 Wednesday=3,
 Thursday=4,
 Friday=5,
 Saturday=6,
 Sunday=7,
 } 
 static void Main(string[] args)
 {
 Console.WriteLine("Weekdays position");
 Console.WriteLine("Sunday {0}", (int) Weekdays.Sunday);
 Console.WriteLine("Monday {0}", (int) Weekdays.Monday);
 Console.WriteLine("Tuesday {0}", Weekdays.Tuesday);
 Console.WriteLine("Wednesday {0}", (int)Weekdays.Wednesday);
 Console.WriteLine("Thursday {0}", (int)Weekdays.Thursday);
 Console.WriteLine("Friday {0}", (int)Weekdays.Friday);
 Console.WriteLine("Saturday {0}", (int)Weekdays.Saturday);
 Console.ReadLine();


 }
 }
}

Run the application and look at the output screen.

Enumeration

Download the complete source code for the application here.

Create another example to get a better understanding of Enumeration:

Example2:

Create a Console Application project called “EnumerationDemo2”.

Enumeration

Write the following code in Program.cs.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EnumerationDemo2
{
 class Program
 {
 public enum TimeOfDay
 {
 Morning = 0,
 Afternoon = 1,
 Evening = 2
 }

static void Main(string[] args)
 {
 GreetingMessage(TimeOfDay.Morning);
 GreetingMessage(TimeOfDay.Evening);
 GreetingMessage(TimeOfDay.Afternoon);
 Console.ReadKey();


 }

static void GreetingMessage(TimeOfDay timeOfDay)
 {
 switch (timeOfDay)
 {
 case TimeOfDay.Morning:
 Console.WriteLine("Good morning!");
 break;
 case TimeOfDay.Afternoon:
 Console.WriteLine("Good afternoon!");
 break;
 case TimeOfDay.Evening:
 Console.WriteLine("Good evening!");
 break;
 default:
 Console.WriteLine("Hello!");
 break;
 }
 }

}
}

Run the application and look at the output screen.

Enumeration

Download the complete source code for Example 2 from here.

Hope you understand what is enumeration in C#. Your feedback is valuable for me.

Read More:

Thank You.

Explore post tags

69 Comments

  • I just want to say I am new to blogging and site-building and definitely liked your blog. Very likely I’m going to bookmark your blog . You surely have good stories. Many thanks for sharing with us your webpage.

  • I have observed that in the world these days, video games include the latest rage with kids of all ages. There are occassions when it may be difficult to drag your kids away from the activities. If you want the best of both worlds, there are plenty of educational gaming activities for kids. Thanks for your post.

  • Faytech North America is a touch screen Manufacturer of both monitors and pcs. They specialize in the design, development, manufacturing and marketing of Capacitive touch screen, Resistive touch screen, Industrial touch screen, IP65 touch screen, touchscreen monitors and integrated touchscreen PCs. Contact them at http://www.faytech.us, 121 Varick Street, New York, NY 10013, +1 646 205 3214.

  • Sapid Agency is a Search Engine Optimization company in New York City that provides SEO Services. Their proprietary SEO strategies help struggling websites and aspiring business owners to rank their websites higher in multiple search engines like Google , Yahoo and Bing. They provide local and gmb map ranking for businesses in NYC and many other local areas. Find more at https://www.sapidagency.com/ @ 145 E 57TH NEW YORK, NY 10022, USA, +1 971 341 5608 USA

  • May I simply just say what a relief to find an individual who genuinely understands what they are discussing on the internet. You definitely know how to bring an issue to light and make it important. More and more people really need to check this out and understand this side of your story. I was surprised you are not more popular because you most certainly possess the gift.

  • Hi, I do believe this is an excellent website. I stumbledupon it 😉 I’m going to return yet again since i have book-marked it. Money and freedom is the greatest way to change, may you be rich and continue to help others.

  • You are so cool! I don’t suppose I have read anything like this before. So nice to discover another person with some original thoughts on this issue. Really.. many thanks for starting this up. This web site is something that is required on the web, someone with a little originality.

  • An additional issue is that video games are normally serious in nature with the key focus on learning rather than fun. Although, it comes with an entertainment factor to keep your sons or daughters engaged, every single game is usually designed to focus on a specific set of skills or course, such as mathematics or scientific discipline. Thanks for your article.

  • I blog often and I really thank you for your information. Your article has truly peaked my interest. I am going to take a note of your site and keep checking for new information about once a week. I subscribed to your Feed as well.

  • Aw, this was a really good post. Taking a few minutes and actual effort to produce a top notch article… but what can I say… I hesitate a whole lot and never seem to get nearly anything done.

  • Very good information. Lucky me I found your blog by accident (stumbleupon). I have saved it for later!

  • Oh my goodness! Impressive article dude! Many thanks, However I am going through difficulties with your RSS. I don’t know why I can’t subscribe to it. Is there anyone else having similar RSS problems? Anyone that knows the solution will you kindly respond? Thanks!

  • Having read this I believed it was rather informative. I appreciate you spending some time and energy to put this short article together. I once again find myself spending way too much time both reading and commenting. But so what, it was still worth it!

  • After looking into a handful of the blog articles on your web site, I really like your way of writing a blog. I saved as a favorite it to my bookmark site list and will be checking back in the near future. Take a look at my website as well and tell me what you think.

  • This website was… how do you say it? Relevant!! Finally I’ve found something that helped me. Many thanks.

  • I’d like to thank you for the efforts you have put in penning this website. I’m hoping to see the same high-grade blog posts by you in the future as well. In fact, your creative writing abilities has inspired me to get my own website now 😉

  • Oh my goodness! Amazing article dude! Many thanks, However I am encountering problems with your RSS. I don’t know the reason why I can’t join it. Is there anybody else getting identical RSS issues? Anyone who knows the answer will you kindly respond? Thanks.

  • It’s practically unattainable to come across well-informed men and women on this content, but you appear like you be aware of those things you’re revealing! Cheers

  • You are so interesting! I don’t think I’ve truly read a single thing like that before. So good to find somebody with original thoughts on this subject matter. Really.. thank you for starting this up. This web site is one thing that’s needed on the internet, someone with some originality.

  • Generally I don’t read article on blogs, however I wish to say that this write-up very forced me to check out and do so! Your writing style has been amazed me. Thank you, quite nice article.

  • Howdy! I could have sworn I’ve been to this site before but after going through a few of the posts I realized it’s new to me. Anyhow, I’m definitely happy I came across it and I’ll be bookmarking it and checking back often!

  • Way cool! Some very valid points! I appreciate you penning this article and also the rest of the website is very good.

  • I truly love your website.. Great colors & theme. Did you make this amazing site yourself? Please reply back as I’m looking to create my own personal website and would like to know where you got this from or just what the theme is called. Many thanks.

  • I?m impressed, I have to say. Really hardly ever do I experience a blog site that?s both informative and also enjoyable, and also let me inform you, you have actually struck the nail on the head. Your idea is outstanding; the problem is something that not nearly enough people are talking smartly around. I am very pleased that I stumbled across this in my look for something associating with this.

  • hi!,I like your writing so much! percentage we keep in touch extra approximately your article on AOL? I need a specialist on this space to solve my problem. Maybe that’s you! Taking a look forward to peer you.

  • Oh my goodness! Impressive article dude! Thank you so much, However I am experiencing difficulties with your RSS. I don’t know why I can’t join it. Is there anyone else getting similar RSS problems? Anybody who knows the answer can you kindly respond? Thanks.

  • Thank you, I’ve just been searching for info about this topic for a while and yours is the best I have came upon so far. But, what about the conclusion? Are you certain in regards to the source?

  • This website certainly has all the information and facts I wanted about this subject and didn’t know who to ask.

  • I need to to thank you for this good read!! I certainly loved every little bit of it. I have got you book marked to look at new things you post…

  • It certainly is practically close to impossible to come across well-advised visitors on this theme, regrettably you look like you fully understand what exactly you’re writing about! Appreciation

  • Hi! I just would like to offer you a huge thumbs up for the great information you have here on this post. I am returning to your web site for more soon.

  • It is actually mostly unthinkable to see well-qualified parties on this content, however you seem like you fully understand exactly what you’re writing on! Thanks

  • Sapid Agency is a Search Engine Optimization company in New York City that provides SEO Services. Their proprietary SEO strategies help struggling websites and aspiring business owners to rank their websites higher in multiple search engines like Google , Yahoo and Bing. They provide local and gmb map ranking for businesses in NYC and many other local areas. Find more at https://www.sapidagency.com/ @ 145 E 57TH NEW YORK, NY 10022, USA, +1 971 341 5608 USA

  • Faytech North America is a touch screen Manufacturer of both monitors and pcs. They specialize in the design, development, manufacturing and marketing of Capacitive touch screen, Resistive touch screen, Industrial touch screen, IP65 touch screen, touchscreen monitors and integrated touchscreen PCs. Contact them at http://www.faytech.us, 121 Varick Street, New York, NY 10013, +1 646 205 3214.

  • I wanted to thank you for this wonderful read!! I certainly enjoyed every bit of it. I have got you saved as a favorite to check out new things you post…

  • I really want to reveal to you that I am new to putting up a blog and pretty much cherished your review. Probably I am most likely to bookmark your blog post . You certainly have extraordinary article materials. Admire it for share-out with us all of your website post

  • Nice post. I learn something new and challenging on sites I stumbleupon on a daily basis. It’s always exciting to read through content from other writers and practice a little something from other web sites.

  • MetroClick specializes in building completely interactive products like Photo Booth for rental or sale, Touch Screen Kiosks, Large Touch Screen Displays , Monitors, Digital Signages and experiences. With our own hardware production facility and in-house software development teams, we are able to achieve the highest level of customization and versatility for Photo Booths, Touch Screen Kiosks, Touch Screen Monitors and Digital Signage. Visit MetroClick in NYC at http://www.metroclick.com/ or , 121 Varick St, New York, NY 10013, +1 646-843-0888.

  • It is actually mostly extremely difficult to encounter well-educated viewers on this issue, still, you come across as like you comprehend the things that you’re covering! With Thanks

  • It truly is mostly extremely difficult to come across well-informed individual on this content, however , you seem like you comprehend exactly what you’re covering! Excellent

  • I really hope to share it with you that I am new to posting and pretty much liked your website. Likely I am probably to save your blog post . You literally have excellent article materials. Value it for discussing with us your main internet site page

  • Hey there! Do you use Twitter? I’d like to follow you if that would be okay. I’m undoubtedly enjoying your blog and look forward to new updates.|